From 5e5a049f2b87fb019b3013612b97a6ff75b70ef9 Mon Sep 17 00:00:00 2001 From: ductnn Date: Wed, 13 Dec 2023 09:28:05 +0700 Subject: [PATCH] add sol --- .../searchSuggestionsSystem.go | 57 +++++++++++++++ .../implementTriePrefixTree.go | 72 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 leetcode/design/1268.SearchSuggestionsSystem/searchSuggestionsSystem.go create mode 100644 leetcode/design/208.ImplementTriePrefixTree/implementTriePrefixTree.go diff --git a/leetcode/design/1268.SearchSuggestionsSystem/searchSuggestionsSystem.go b/leetcode/design/1268.SearchSuggestionsSystem/searchSuggestionsSystem.go new file mode 100644 index 0000000..952f88c --- /dev/null +++ b/leetcode/design/1268.SearchSuggestionsSystem/searchSuggestionsSystem.go @@ -0,0 +1,57 @@ +package main + +import "sort" + +type Trie struct { + children [26]*Trie + v []int +} + +func newTrie() *Trie { + return &Trie{} +} +func (this *Trie) insert(w string, i int) { + node := this + for _, c := range w { + c -= 'a' + if node.children[c] == nil { + node.children[c] = newTrie() + } + node = node.children[c] + if len(node.v) < 3 { + node.v = append(node.v, i) + } + } +} + +func (this *Trie) search(w string) [][]int { + node := this + n := len(w) + result := make([][]int, n) + for i, c := range w { + c -= 'a' + if node.children[c] == nil { + break + } + node = node.children[c] + result[i] = node.v + } + return result +} + +func suggestedProducts(products []string, searchWord string) [][]string { + result := [][]string{} + sort.Strings(products) + trie := newTrie() + for i, w := range products { + trie.insert(w, i) + } + for _, v := range trie.search(searchWord) { + t := []string{} + for _, i := range v { + t = append(t, products[i]) + } + result = append(result, t) + } + return result +} diff --git a/leetcode/design/208.ImplementTriePrefixTree/implementTriePrefixTree.go b/leetcode/design/208.ImplementTriePrefixTree/implementTriePrefixTree.go new file mode 100644 index 0000000..0e7e34c --- /dev/null +++ b/leetcode/design/208.ImplementTriePrefixTree/implementTriePrefixTree.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" +) + +// TrieNode represents a node in the Trie data structure +type TrieNode struct { + children [26]*TrieNode + isEnd bool +} + +// Trie represents the Trie data structure +type Trie struct { + root *TrieNode +} + +// Constructor initializes the Trie +func Constructor() Trie { + return Trie{root: &TrieNode{}} +} + +// Insert adds a word into the Trie +func (t *Trie) Insert(word string) { + node := t.root + for _, ch := range word { + index := ch - 'a' + if node.children[index] == nil { + node.children[index] = &TrieNode{} + } + node = node.children[index] + } + node.isEnd = true +} + +// Search returns if the word is in the Trie +func (t *Trie) Search(word string) bool { + node := t.root + for _, ch := range word { + index := ch - 'a' + if node.children[index] == nil { + return false + } + node = node.children[index] + } + return node != nil && node.isEnd +} + +// StartsWith returns if there is any word in the Trie that starts with the given prefix +func (t *Trie) StartsWith(prefix string) bool { + node := t.root + for _, ch := range prefix { + index := ch - 'a' + if node.children[index] == nil { + return false + } + node = node.children[index] + } + return node != nil +} + +func main() { + trie := Constructor() + + trie.Insert("apple") + fmt.Println(trie.Search("apple")) // Output: true + fmt.Println(trie.Search("app")) // Output: false + fmt.Println(trie.StartsWith("app")) // Output: true + + trie.Insert("app") + fmt.Println(trie.Search("app")) // Output: true +}