-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd-and-search-word-data-structure-design.cpp
81 lines (66 loc) · 1.81 KB
/
add-and-search-word-data-structure-design.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
//Runtime: 79 ms
class WordDictionary {
public:
/** Initialize your data structure here. */
typedef struct TrieNode
{
int isLeaf;
struct TrieNode* children[26];
}TrieNode;
TrieNode* root = NULL;
TrieNode* NewNode()
{
TrieNode* newNode = new TrieNode;
newNode->isLeaf = 0;
for(int i=0;i<26;i++)
newNode->children[i] = NULL;
return newNode;
}
WordDictionary() {
root = NewNode();
}
/** Adds a word into the data structure. */
void addWord(string word) {
TrieNode* crawl = root;
for(char c:word)
{
if(!crawl->children[c-'a'])
crawl->children[c-'a'] = NewNode();
crawl = crawl->children[c-'a'];
}
crawl->isLeaf = 1;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool searchHelp(string word, TrieNode* crawl)
{
if(!crawl)
return 0;
for(int c=0;c<word.size();c++)
{
if(word[c] == '.')
{
for(int i=0;i<26;i++)
if(searchHelp(word.substr(c+1), crawl->children[i]))
return 1;
return 0;
}
else
{
if(!crawl->children[word[c]-'a'])
return 0;
}
crawl = crawl->children[word[c]-'a'];
}
return crawl->isLeaf;
}
bool search(string word) {
TrieNode* crawl = root;
return searchHelp(word, crawl);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/