LC.P1268[搜索推荐系统]

方法一:排序+Trie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
Trie trie = new Trie();
Arrays.sort(products);
for (int i = 0; i < products.length; ++i) trie.insert(products[i], i);
List<List<String>> ans = new ArrayList<>();
StringBuilder prefix = new StringBuilder();
for (int i = 0; i < searchWord.length(); ++i) {
prefix.append(searchWord.charAt(i));
List<String> list = new ArrayList<>();
int[] query = trie.query(prefix);
int left = query[0], right = query[1];
if (left != -1) {
for (int j = left; j <= Math.min(left + 2, right); ++j) list.add(products[j]);
}
ans.add(list);
}
return ans;
}

private static class Trie {
Trie[] next;
boolean isEnd;
Map<Trie, Integer> minMap = new HashMap<>();
Map<Trie, Integer> maxMap = new HashMap<>();

public Trie() {
next = new Trie[26];
isEnd = false;
}

public void insert(String word, int index) {
Trie node = this;
for (int i = 0; i < word.length(); ++i) {
int u = word.charAt(i) - 'a';
if (node.next[u] == null) {
node.next[u] = new Trie();
minMap.put(node.next[u], index);
}
maxMap.put(node.next[u], index);
node = node.next[u];
}
node.isEnd = true;
}

public int[] query(StringBuilder word) {
Trie node = this;
int left = -1, right = -1;
for (int i = 0; i < word.length(); ++i) {
int u = word.charAt(i) - 'a';
node = node.next[u];
if (node == null) return new int[]{-1, -1};
left = minMap.get(node);
right = maxMap.get(node);

}
return new int[]{left, right};
}
}
}
  • 时间复杂度:$O(\sum_{i = 0}^{n - 1}products[i].length + m ^ 2)$,$m$为$searchWord$的长度
  • 空间复杂度:$O(\sum_{i = 0}^{n - 1}products[i].length \times C)$,$C = 26$