阅读设置
界面、侧栏和正文分别调整,自动保存。
主题切换
界面、侧栏和正文分别调整,自动保存。
问题描述: 给定两个字符串数组 wordsContainer 和 wordsQuery。 对每个查询字符串 wordsQuery[i],在 wordsContainer 中找到与它有最长公共后缀的字符串,返回其下标。 如果有多个字符串满足条件:
示例:
输入:
wordsContainer = ["abcd","bcd","xbcd"],wordsQuery = ["cd","bcd","xyz"]输出:[1,1,1]
wordsContainer 中的每个字符串逆序插入字典树;wordsQuery[i] 逆序遍历字典树,找到最深能匹配的节点,该节点存储的下标即为答案。package lc3000_lc3099.lc3093;
public class Solution {
class TrieNode {
TrieNode[] children;
Integer index;
public TrieNode(Integer index) {
children = new TrieNode[26];
this.index = index;
}
}
public class Trie {
private TrieNode root;
public Trie(Integer index) {
root = new TrieNode(index);
}
}
public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
int n = wordsContainer.length;
int m = wordsQuery.length;
int[] res = new int[m];
int minLen = Integer.MAX_VALUE;
int defaultIdx = 0;
for (int i = 0; i < n; i++) {
if (wordsContainer[i].length() < minLen) {
minLen = wordsContainer[i].length();
defaultIdx = i;
}
}
Trie trie = new Trie(defaultIdx);
for (int i = 0; i < n; i++) {
String s = wordsContainer[i];
method2(i, s, trie.root, wordsContainer);
}
for (int i = 0; i < m; i++) {
String s = wordsQuery[i];
res[i] = method1(s, trie.root);
}
return res;
}
private void method2(int index, String s, TrieNode node, String[] wordsContainer) {
TrieNode curNode = node;
int n = s.length();
for (int i = n - 1; i >= 0; i--) {
char c = s.charAt(i);
int num = c - 'a';
if (curNode.children[num] == null) {
curNode.children[num] = new TrieNode(index);
} else {
int oldIdx = curNode.children[num].index;
int oldLen = wordsContainer[oldIdx].length();
int newLen = wordsContainer[index].length();
if (newLen < oldLen || (newLen == oldLen && index < oldIdx)) {
curNode.children[num].index = index;
}
}
curNode = curNode.children[num];
}
}
private int method1(String s, TrieNode node) {
int res = node.index;
int n = s.length();
TrieNode curNode = node;
for (int i = n - 1; i >= 0; i--) {
char c = s.charAt(i);
int num = c - 'a';
if (curNode.children[num] == null) {
break;
}
curNode = curNode.children[num];
res = curNode.index;
}
return res;
}
}(代码未做任何修改,仅添加注释讲解)
class Solution {
// 字典树节点:记录子节点、当前路径下最短且最靠前的字符串下标
class Node {
Node[] children = new Node[26];
int minLen = Integer.MAX_VALUE; // 该路径下匹配的最短字符串长度
int idx = -1; // 该路径下匹配的最优字符串下标
}
Node root = new Node();
public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
// 1. 构建字典树:将所有字符串逆序插入
for (int i = 0; i < wordsContainer.length; i++) {
String word = wordsContainer[i];
Node cur = root;
// 根节点记录全局最优(无公共后缀时的默认答案)
if (word.length() < cur.minLen) {
cur.minLen = word.length();
cur.idx = i;
}
// 从后往前遍历字符串(逆序插入,把后缀变成前缀)
for (int j = word.length() - 1; j >= 0; j--) {
char c = word.charAt(j);
if (cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new Node();
}
cur = cur.children[c - 'a'];
// 更新当前节点的最优下标:长度更短/长度相同但下标更小
if (word.length() < cur.minLen) {
cur.minLen = word.length();
cur.idx = i;
}
}
}
// 2. 查询每个字符串的最长公共后缀
int[] res = new int[wordsQuery.length];
for (int i = 0; i < wordsQuery.length; i++) {
String word = wordsQuery[i];
Node cur = root;
// 从后往前遍历查询字符串,在字典树中匹配
for (int j = word.length() - 1; j >= 0; j--) {
char c = word.charAt(j);
if (cur.children[c - 'a'] == null) {
break; // 匹配中断,退出循环
}
cur = cur.children[c - 'a'];
}
// 当前节点存储的idx就是最优答案
res[i] = cur.idx;
}
return res;
}
}wordsContainer 长度,wordsQuery 长度,