From fbb6dd8cebf964871a2deca9d3669ab8e6ea8d9a Mon Sep 17 00:00:00 2001 From: superq <13815865+meooxx@users.noreply.github.com> Date: Sat, 16 Mar 2024 21:55:30 +0800 Subject: [PATCH] review300: design Add and search words data structure --- .../review300/review300.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 designAddAndSearchWordsDataStructure/review300/review300.go diff --git a/designAddAndSearchWordsDataStructure/review300/review300.go b/designAddAndSearchWordsDataStructure/review300/review300.go new file mode 100644 index 0000000..dcb2f82 --- /dev/null +++ b/designAddAndSearchWordsDataStructure/review300/review300.go @@ -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); + */