Skip to content

Commit 730f6ab

Browse files
committed
feat: add solutions to lc problem: No.1268
No.1268.Search Suggestions System
1 parent 2af285e commit 730f6ab

File tree

5 files changed

+445
-2
lines changed

5 files changed

+445
-2
lines changed

Diff for: solution/1200-1299/1268.Search Suggestions System/README.md

+151-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,172 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:排序 + 前缀树**
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
<!-- 这里可写当前语言的特殊实现逻辑 -->
7173

7274
```python
73-
75+
class Trie:
76+
def __init__(self):
77+
self.children = [None] * 26
78+
self.v = []
79+
80+
def insert(self, word, i):
81+
node = self
82+
for c in word:
83+
idx = ord(c) - ord('a')
84+
if node.children[idx] is None:
85+
node.children[idx] = Trie()
86+
node = node.children[idx]
87+
node.v.append(i)
88+
89+
def search(self, word):
90+
res = [[] for _ in range(len(word))]
91+
node = self
92+
for i, c in enumerate(word):
93+
idx = ord(c) - ord('a')
94+
if node.children[idx] is None:
95+
break
96+
node = node.children[idx]
97+
res[i] = node.v[:3]
98+
return res
99+
100+
101+
class Solution:
102+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
103+
products.sort()
104+
trie = Trie()
105+
for i, w in enumerate(products):
106+
trie.insert(w, i)
107+
res = trie.search(searchWord)
108+
return [[products[j] for j in v] for v in res]
74109
```
75110

76111
### **Java**
77112

78113
<!-- 这里可写当前语言的特殊实现逻辑 -->
79114

80115
```java
116+
class Trie {
117+
Trie[] children = new Trie[26];
118+
List<Integer> v = new ArrayList<>();
119+
120+
void insert(String word, int i) {
121+
Trie node = this;
122+
for (char c : word.toCharArray()) {
123+
c -= 'a';
124+
if (node.children[c] == null) {
125+
node.children[c] = new Trie();
126+
}
127+
node = node.children[c];
128+
if (node.v.size() < 3) {
129+
node.v.add(i);
130+
}
131+
}
132+
}
133+
134+
List<List<Integer>> search(String word) {
135+
List<List<Integer>> res = new ArrayList<>();
136+
int n = word.length();
137+
for (int i = 0; i < n; ++i) {
138+
res.add(new ArrayList<>());
139+
}
140+
Trie node = this;
141+
for (int i = 0; i < n; ++i) {
142+
char c = word.charAt(i);
143+
c -= 'a';
144+
if (node.children[c] == null) {
145+
break;
146+
}
147+
node = node.children[c];
148+
res.set(i, node.v);
149+
}
150+
return res;
151+
}
152+
}
153+
154+
class Solution {
155+
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
156+
Arrays.sort(products);
157+
Trie trie = new Trie();
158+
for (int i = 0; i < products.length; ++i) {
159+
trie.insert(products[i], i);
160+
}
161+
List<List<Integer>> res = trie.search(searchWord);
162+
List<List<String>> ans = new ArrayList<>();
163+
for (List<Integer> v : res) {
164+
List<String> t = new ArrayList<>();
165+
for (int i : v) {
166+
t.add(products[i]);
167+
}
168+
ans.add(t);
169+
}
170+
return ans;
171+
}
172+
}
173+
```
81174

175+
### **Go**
176+
177+
```go
178+
type Trie struct {
179+
children [26]*Trie
180+
v []int
181+
}
182+
183+
func newTrie() *Trie {
184+
return &Trie{}
185+
}
186+
func (this *Trie) insert(word string, i int) {
187+
node := this
188+
for _, c := range word {
189+
c -= 'a'
190+
if node.children[c] == nil {
191+
node.children[c] = newTrie()
192+
}
193+
node = node.children[c]
194+
if len(node.v) < 3 {
195+
node.v = append(node.v, i)
196+
}
197+
}
198+
}
199+
200+
func (this *Trie) search(word string) [][]int {
201+
node := this
202+
n := len(word)
203+
res := make([][]int, n)
204+
for i, c := range word {
205+
c -= 'a'
206+
if node.children[c] == nil {
207+
break
208+
}
209+
node = node.children[c]
210+
res[i] = node.v
211+
}
212+
return res
213+
}
214+
215+
func suggestedProducts(products []string, searchWord string) [][]string {
216+
sort.Strings(products)
217+
trie := newTrie()
218+
for i, w := range products {
219+
trie.insert(w, i)
220+
}
221+
res := trie.search(searchWord)
222+
var ans [][]string
223+
for _, v := range res {
224+
t := []string{}
225+
for _, i := range v {
226+
t = append(t, products[i])
227+
}
228+
ans = append(ans, t)
229+
}
230+
return ans
231+
}
82232
```
83233

84234
### **...**

Diff for: solution/1200-1299/1268.Search Suggestions System/README_EN.md

+149-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,161 @@ After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mo
6161
### **Python3**
6262

6363
```python
64-
64+
class Trie:
65+
def __init__(self):
66+
self.children = [None] * 26
67+
self.v = []
68+
69+
def insert(self, word, i):
70+
node = self
71+
for c in word:
72+
idx = ord(c) - ord('a')
73+
if node.children[idx] is None:
74+
node.children[idx] = Trie()
75+
node = node.children[idx]
76+
node.v.append(i)
77+
78+
def search(self, word):
79+
res = [[] for _ in range(len(word))]
80+
node = self
81+
for i, c in enumerate(word):
82+
idx = ord(c) - ord('a')
83+
if node.children[idx] is None:
84+
break
85+
node = node.children[idx]
86+
res[i] = node.v[:3]
87+
return res
88+
89+
90+
class Solution:
91+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
92+
products.sort()
93+
trie = Trie()
94+
for i, w in enumerate(products):
95+
trie.insert(w, i)
96+
res = trie.search(searchWord)
97+
return [[products[j] for j in v] for v in res]
6598
```
6699

67100
### **Java**
68101

69102
```java
103+
class Trie {
104+
Trie[] children = new Trie[26];
105+
List<Integer> v = new ArrayList<>();
106+
107+
void insert(String word, int i) {
108+
Trie node = this;
109+
for (char c : word.toCharArray()) {
110+
c -= 'a';
111+
if (node.children[c] == null) {
112+
node.children[c] = new Trie();
113+
}
114+
node = node.children[c];
115+
if (node.v.size() < 3) {
116+
node.v.add(i);
117+
}
118+
}
119+
}
120+
121+
List<List<Integer>> search(String word) {
122+
List<List<Integer>> res = new ArrayList<>();
123+
int n = word.length();
124+
for (int i = 0; i < n; ++i) {
125+
res.add(new ArrayList<>());
126+
}
127+
Trie node = this;
128+
for (int i = 0; i < n; ++i) {
129+
char c = word.charAt(i);
130+
c -= 'a';
131+
if (node.children[c] == null) {
132+
break;
133+
}
134+
node = node.children[c];
135+
res.set(i, node.v);
136+
}
137+
return res;
138+
}
139+
}
140+
141+
class Solution {
142+
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
143+
Arrays.sort(products);
144+
Trie trie = new Trie();
145+
for (int i = 0; i < products.length; ++i) {
146+
trie.insert(products[i], i);
147+
}
148+
List<List<Integer>> res = trie.search(searchWord);
149+
List<List<String>> ans = new ArrayList<>();
150+
for (List<Integer> v : res) {
151+
List<String> t = new ArrayList<>();
152+
for (int i : v) {
153+
t.add(products[i]);
154+
}
155+
ans.add(t);
156+
}
157+
return ans;
158+
}
159+
}
160+
```
70161

162+
### **Go**
163+
164+
```go
165+
type Trie struct {
166+
children [26]*Trie
167+
v []int
168+
}
169+
170+
func newTrie() *Trie {
171+
return &Trie{}
172+
}
173+
func (this *Trie) insert(word string, i int) {
174+
node := this
175+
for _, c := range word {
176+
c -= 'a'
177+
if node.children[c] == nil {
178+
node.children[c] = newTrie()
179+
}
180+
node = node.children[c]
181+
if len(node.v) < 3 {
182+
node.v = append(node.v, i)
183+
}
184+
}
185+
}
186+
187+
func (this *Trie) search(word string) [][]int {
188+
node := this
189+
n := len(word)
190+
res := make([][]int, n)
191+
for i, c := range word {
192+
c -= 'a'
193+
if node.children[c] == nil {
194+
break
195+
}
196+
node = node.children[c]
197+
res[i] = node.v
198+
}
199+
return res
200+
}
201+
202+
func suggestedProducts(products []string, searchWord string) [][]string {
203+
sort.Strings(products)
204+
trie := newTrie()
205+
for i, w := range products {
206+
trie.insert(w, i)
207+
}
208+
res := trie.search(searchWord)
209+
var ans [][]string
210+
for _, v := range res {
211+
t := []string{}
212+
for _, i := range v {
213+
t = append(t, products[i])
214+
}
215+
ans = append(ans, t)
216+
}
217+
return ans
218+
}
71219
```
72220

73221
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
type Trie struct {
2+
children [26]*Trie
3+
v []int
4+
}
5+
6+
func newTrie() *Trie {
7+
return &Trie{}
8+
}
9+
func (this *Trie) insert(word string, i int) {
10+
node := this
11+
for _, c := range word {
12+
c -= 'a'
13+
if node.children[c] == nil {
14+
node.children[c] = newTrie()
15+
}
16+
node = node.children[c]
17+
if len(node.v) < 3 {
18+
node.v = append(node.v, i)
19+
}
20+
}
21+
}
22+
23+
func (this *Trie) search(word string) [][]int {
24+
node := this
25+
n := len(word)
26+
res := make([][]int, n)
27+
for i, c := range word {
28+
c -= 'a'
29+
if node.children[c] == nil {
30+
break
31+
}
32+
node = node.children[c]
33+
res[i] = node.v
34+
}
35+
return res
36+
}
37+
38+
func suggestedProducts(products []string, searchWord string) [][]string {
39+
sort.Strings(products)
40+
trie := newTrie()
41+
for i, w := range products {
42+
trie.insert(w, i)
43+
}
44+
res := trie.search(searchWord)
45+
var ans [][]string
46+
for _, v := range res {
47+
t := []string{}
48+
for _, i := range v {
49+
t = append(t, products[i])
50+
}
51+
ans = append(ans, t)
52+
}
53+
return ans
54+
}

0 commit comments

Comments
 (0)