211. Design Add and Search Words Data Structure

211. Design Add and Search Words Data Structure

Description

Difficulty: Medium

Related Topics: String, Depth-First Search, Design, Trie

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

  • WordDictionary() Initializes the object.
  • void addWord(word) Adds word to the data structure, it can be matched later.
  • bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output
[null,null,null,null,false,true,true,true]

Explanation
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("mad");
wordDictionary.search("pad"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True

Constraints:

  • 1 <= word.length <= 25
  • word in addWord consists of lowercase English letters.
  • word in search consist of '.' or lowercase English letters.
  • There will be at most 2 dots in word for search queries.
  • At most 104 calls will be made to addWord and search.

Hints/Notes

  • 2023/10/07
  • Trie
  • Need a for loop for wildcard matching, also need a helper function tracking the index
  • No solution from 0x3F

Solution

Language: C++

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
class WordDictionary {
public:
struct TrieNode {
bool end = false;
TrieNode* children[26]{};
};

TrieNode* root;

WordDictionary() {
root = new TrieNode();
}

void addWord(string word) {
TrieNode* head = root;
for (char c : word) {
if (head->children[c - 'a'] == nullptr) {
head->children[c - 'a'] = new TrieNode();
}
head = head->children[c - 'a'];
}
head->end = true;
}

bool search(string word) {
return searchWithPatern(root, 0, word);
}

bool searchWithPatern(TrieNode* head, int index, string& word) {
if (!head) {
return false;
}
if (index == word.size()) {
return head->end;
}
char c = word[index];
if (c != '.') {
return searchWithPatern(head->children[c - 'a'], index + 1, word);
} else {
for (TrieNode* node : head->children) {
if (node) {
bool branchMatch = searchWithPatern(node, index + 1, word);
if (branchMatch) {
return true;
}
}
}
return false;
}
}
};

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/