-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path676.cpp
62 lines (56 loc) · 1.44 KB
/
676.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// trie.cpp
class MagicDictionary {
struct trie {
trie() : isWord(false) { memset(this, 0, sizeof(trie)); }
bool isWord;
trie *children[26];
} root;
void insert(string &word) {
trie *p = &root;
for (auto ch : word) {
int offset = ch - 'a';
if (p->children[offset] == nullptr)
p->children[offset] = new trie();
p = p->children[offset];
}
p->isWord = true;
}
bool _search(string word) {
trie *p = &root;
for (auto ch : word) {
int offset = ch - 'a';
if (p->children[offset] == nullptr)
return false;
p = p->children[offset];
}
return p->isWord;
}
public:
/** Initialize your data structure here. */
MagicDictionary() {}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
for (string &str : dict)
insert(str);
}
/** Returns if there is any word in the trie that equals to the given word
* after modifying exactly one character */
bool search(string word) {
for (int i = 0; i < word.size(); ++i) {
char old = word[i];
for (char j = 'a'; j <= 'z'; ++j) {
word[i] = j;
if (j != old && _search(word))
return true;
}
word[i] = old;
}
return false;
}
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* bool param_2 = obj.search(word);
*/