File tree Expand file tree Collapse file tree 1 file changed +2
-59
lines changed
Solution/1268. Search Suggestions System Expand file tree Collapse file tree 1 file changed +2
-59
lines changed Original file line number Diff line number Diff line change 11---
22comments : true
33difficulty : Medium
4- edit_url : https://github.com/doocs/leetcode/edit/main/solution/1200-1299/1268.Search%20Suggestions%20System/README_EN.md
4+ edit_url : Antim
55rating : 1573
66source : Weekly Contest 164 Q3
77tags :
1717
1818# [ 1268. Search Suggestions System] ( https://leetcode.com/problems/search-suggestions-system )
1919
20- [ 中文文档 ] ( /solution/1200-1299/1268.Search%20Suggestions%20System/README.md )
20+
2121
2222## Description
2323
@@ -243,63 +243,6 @@ public:
243243};
244244```
245245
246- #### Go
247-
248- ```go
249- type Trie struct {
250- children [26]*Trie
251- v []int
252- }
253-
254- func newTrie() *Trie {
255- return &Trie{}
256- }
257- func (this *Trie) insert(w string, i int) {
258- node := this
259- for _, c := range w {
260- c -= 'a'
261- if node.children[c] == nil {
262- node.children[c] = newTrie()
263- }
264- node = node.children[c]
265- if len(node.v) < 3 {
266- node.v = append(node.v, i)
267- }
268- }
269- }
270-
271- func (this *Trie) search(w string) [][]int {
272- node := this
273- n := len(w)
274- ans := make([][]int, n)
275- for i, c := range w {
276- c -= 'a'
277- if node.children[c] == nil {
278- break
279- }
280- node = node.children[c]
281- ans[i] = node.v
282- }
283- return ans
284- }
285-
286- func suggestedProducts(products []string, searchWord string) (ans [][]string) {
287- sort.Strings(products)
288- trie := newTrie()
289- for i, w := range products {
290- trie.insert(w, i)
291- }
292- for _, v := range trie.search(searchWord) {
293- t := []string{}
294- for _, i := range v {
295- t = append(t, products[i])
296- }
297- ans = append(ans, t)
298- }
299- return
300- }
301- ```
302-
303246<!-- tabs:end -->
304247
305248<!-- solution:end -->
You can’t perform that action at this time.
0 commit comments