Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 13, 2023
1 parent 5e5a049 commit f188183
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
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))
}
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
}
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
}

0 comments on commit f188183

Please sign in to comment.