Skip to content

Commit

Permalink
Merge branch 'master' of github.com:meooxx/leetcode into master
Browse files Browse the repository at this point in the history
  • Loading branch information
meooxx committed Mar 17, 2024
2 parents 2aefc61 + 7bd23ea commit 7f99d31
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions designAddAndSearchWordsDataStructure/review300/review300.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

type WordDictionary struct {
isWord bool
wordMap [26]*WordDictionary
}

func Constructor() WordDictionary {
return WordDictionary{
wordMap: [26]*WordDictionary{},
}

}

func (this *WordDictionary) AddWord(word string) {
for i := range word {
index := int(word[i] - 'a')
if this.wordMap[index] == nil {
this.wordMap[index] = &WordDictionary{}
}
this = this.wordMap[index]

}
this.isWord = true
}

func (this *WordDictionary) Search(word string) bool {
if word == "" {
return this.isWord
}
for i := range word {
index := int(word[i] - 'a')
if word[i] == '.' {
for j := range this.wordMap {
if this.wordMap[j] != nil {
if this.wordMap[j].Search(word[i+1:]) {
return true
}
}

}
return false
} else if this.wordMap[index] != nil {
return this.wordMap[index].Search(word[i+1:])
} else {
return false
}

}

return false

}

/**
* Your WordDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.AddWord(word);
* param_2 := obj.Search(word);
*/

0 comments on commit 7f99d31

Please sign in to comment.