-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
leetcode/leetcode75/Stack/2390.RemovingStarsFromAString/removingStarsFromAString.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func removeStars(s string) string { | ||
result := []rune{} | ||
for _, c := range s { | ||
if c == '*' { | ||
result = result[:len(result)-1] | ||
} else { | ||
result = append(result, c) | ||
} | ||
} | ||
return string(result) | ||
} | ||
|
||
func main() { | ||
s := "leet**cod*e" | ||
fmt.Println(removeStars(s)) | ||
} |
57 changes: 57 additions & 0 deletions
57
leetcode/leetcode75/Trie/1268.SearchSuggestionsSystem/searchSuggestionsSystem.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
72 changes: 72 additions & 0 deletions
72
leetcode/leetcode75/Trie/208.ImplementTriePrefixTree/implementTriePrefixTree.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |