-
Notifications
You must be signed in to change notification settings - Fork 0
/
676. Implement Magic Dictionary.cpp
148 lines (131 loc) · 3.99 KB
/
676. Implement Magic Dictionary.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Solution 1. Hash Table
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
for(auto x: dict) m[x.size()].push_back(x);
}
/** 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(auto x: m[word.size()])
if(oneEditDistance(x, word)) return true;
return false;
}
private:
unordered_map<int, vector<string>>m;
bool oneEditDistance(string& a, string& b){
int diff = 0;
for(int i = 0; i < a.size() && diff <= 1; i++)
if(a[i] != b[i]) diff++;
return diff == 1;
}
};
// Solution 2. Trie
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
root = new TrieNode();
}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
for(auto x: dict) buildTrie(x);
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
TrieNode* p = root;
int diff = 0;
for(int i = 0; i < word.size(); i++){
char c = word[i];
for(int j = 0; j < 26; j++){
if(p->next[j] == p->next[c - 'a']) continue;
if(p->next[j] && find(p->next[j], word.substr(i + 1))) return true;
}
if(p->next[c - 'a']) p = p->next[c - 'a'];
}
return false;
}
private:
struct TrieNode{
bool isWord;
TrieNode* next[26];
TrieNode():isWord(false){
memset(next, NULL, sizeof(next));
}
};
TrieNode* root;
void buildTrie(string s){
TrieNode* p = root;
for(auto c: s){
if(!p->next[c - 'a']) p->next[c - 'a'] = new TrieNode();
p = p->next[c - 'a'];
}
p->isWord = true;
}
bool find(TrieNode* p, string s){
for(auto c: s)
if(p->next[c - 'a']) p = p->next[c - 'a'];
else return false;
return p->isWord;
}
};
class MagicDictionary {
struct TrieNode {
char val;
vector<TrieNode*> next;
bool isWord;
TrieNode(char c): val(c), next(vector<TrieNode*>(26)), isWord(false) {}
};
public:
/** Initialize your data structure here. */
MagicDictionary() {}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
root = new TrieNode(' ');
for (auto& s: dict) {
buildTrie(s);
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
return dfs(root, word, 0, true);
}
private:
TrieNode* root;
bool dfs(TrieNode* p, string& word, int pos, bool canModify) {
if (pos == word.size()) {
return !canModify && p->isWord;
}
bool find = false;
char c = word[pos];
if (p->next[c - 'a']) {
find |= dfs(p->next[c - 'a'], word, pos + 1, canModify);
}
if (canModify) {
for (auto node: p->next) {
if (node && node->val != c) {
find |= dfs(node, word, pos + 1, false);
}
}
}
return find;
}
void buildTrie(string& s) {
TrieNode* p = root;
for (auto c: s) {
if (!p->next[c - 'a']) {
p->next[c - 'a'] = new TrieNode(c);
}
p = p->next[c - 'a'];
}
p->isWord = true;
}
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* bool param_2 = obj.search(word);
*/