-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWordDictionary.cpp
78 lines (69 loc) · 2.48 KB
/
WordDictionary.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
// Problem: https://leetcode.com/problems/design-add-and-search-words-data-structure/
#include <string>
using namespace std;
struct Node {
unordered_map<char, Node*> node_map;
bool is_leaf = false;
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() { this->_head = new Node(); }
void addWord(const string& word) {
Node* curr = this->_head;
Node* prev = curr;
int index = 0;
// Step-I: Search
for (; index < word.size(); ++index) {
char curr_ch = word[index];
if (curr->node_map.find(curr_ch) == curr->node_map.end()) break;
prev = curr;
curr = curr->node_map[curr_ch];
if (curr == nullptr) break;
}
if (index == word.size()) curr->is_leaf = true;
if (curr == nullptr) curr = prev;
// Step-II: Insert.
for (; index < word.size(); ++index) {
char curr_ch = word[index];
curr->node_map[curr_ch] = new Node();
// This is the last character to be inserted.
// Therefore, the new node will be a leaf.
if (index == (word.size() - 1)) {
curr->node_map[curr_ch]->is_leaf = true;
} else {
curr = curr->node_map[curr_ch];
}
}
}
bool search(const string& word) { return searchInt(this->_head, word); }
private:
bool searchInt(Node* node, string word) {
Node* curr = node;
for (int index = 0; index < word.size(); ++index) {
char curr_ch = word[index];
if (curr_ch == '.') {
for (auto it = curr->node_map.begin(); it != curr->node_map.end(); ++it) {
string rem_word = "";
if (index < (word.size() - 1)) {
rem_word = word.substr(index + 1);
}
if (searchInt(it->second, rem_word)) return true;
}
return false;
} else {
if (curr->node_map.find(curr_ch) == curr->node_map.end()) return false;
curr = curr->node_map[curr_ch];
if (curr == nullptr) return false;
}
}
return curr != nullptr && curr->is_leaf;
}
Node* _head = nullptr;
};