website/content.en/ChapterFour/0600~0699/0676.Implement-Magic-Dictionary.md
Implement a magic directory with buildDict, and search methods.
For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary.
For the method search, you'll be given a word, and judge whether if you modify exactly one character into anothercharacter in this word, the modified word is in the dictionary you just built.
Example 1:
Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False
Note:
a-z.Implement a magic dictionary with buildDict and search methods. For the buildDict method, you will be given a list of non-repetitive words to build a dictionary. For the search method, you will be given a word and need to determine whether you can change exactly one letter in this word into another letter so that the newly formed word exists in the dictionary you built.
MagicDictionary data structure. This data structure stores a string array. When performing the Search operation, it needs to determine whether the incoming string can be changed into a string stored in MagicDictionary by changing exactly one character (without adding or deleting characters). If it can, output true; otherwise, output false.
package leetcode
type MagicDictionary struct {
rdict map[int]string
}
/** Initialize your data structure here. */
func Constructor676() MagicDictionary {
return MagicDictionary{rdict: make(map[int]string)}
}
/** Build a dictionary through a list of words */
func (this *MagicDictionary) BuildDict(dict []string) {
for k, v := range dict {
this.rdict[k] = v
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
func (this *MagicDictionary) Search(word string) bool {
for _, v := range this.rdict {
n := 0
if len(word) == len(v) {
for i := 0; i < len(v); i++ {
if word[i] != v[i] {
n += 1
}
}
if n == 1 {
return true
}
}
}
return false
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.BuildDict(dict);
* param_2 := obj.Search(word);
*/