-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
43 lines (43 loc) · 1.25 KB
/
s1.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
// OJ: https://leetcode.com/problems/implement-magic-dictionary/
// Author: github.com/lzl124631x
// Time:
// buildDict: O(W) where W is the size of all content in word.
// search: O(W) since we only branch once in backtracking, it's not O(26^W)
// Space: O(W)
class TrieNode {
public:
TrieNode *next[26] = {};
bool isWord = false;
};
class Trie {
public:
TrieNode root;
void insert(string &word) {
auto node = &root;
for (char c : word) {
if (!node->next[c - 'a']) node->next[c - 'a'] = new TrieNode();
node = node->next[c - 'a'];
}
node->isWord = true;
}
};
class MagicDictionary {
private:
Trie trie;
bool search(string &word, int index, int diff, TrieNode *node) {
if (diff > 1) return false;
if (index == word.size()) return node->isWord && diff == 1;
for (int i = 0; i < 26; ++i) {
if (!node->next[i]) continue;
if (search(word, index + 1, diff + (word[index] - 'a' == i ? 0 : 1), node->next[i])) return true;
}
return false;
}
public:
void buildDict(vector<string> dict) {
for (auto &word : dict) trie.insert(word);
}
bool search(string word) {
return search(word, 0, 0, &trie.root);
}
};