211. Design Add and Search Words Data Structure
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)
Addsword
to the data structure, it can be matched later.bool search(word)
Returnstrue
if there is any string in the data structure that matchesword
orfalse
otherwise.word
may contain dots'.'
where dots can be matched with any letter.
Example:
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
inaddWord
consists of lowercase English letters.word
insearch
consist of'.'
or lowercase English letters.- There will be at most
2
dots inword
forsearch
queries. - At most
104
calls will be made toaddWord
andsearch
.
Solution:
class WordDictionary {
class TrieNode{
TrieNode[] children = new TrieNode[26];
boolean isWord = false;
}
TrieNode root;
public WordDictionary() {
root = new TrieNode();
}
public void addWord(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++){
TrieNode next = cur.children[word.charAt(i) - 'a'];
if (next == null){
next = new TrieNode();
cur.children[word.charAt(i) - 'a'] = next;
}
cur = next;
}
cur.isWord = true;
}
public boolean search(String word) {
int start = 0;
return helper(word, root, start);
}
public boolean helper(String word, TrieNode cur, int start){
for (int i = start; i < word.length(); i++){
char ch = word.charAt(i);
if (ch == '.'){
for (TrieNode next : cur.children){
if (next != null && helper(word, next, i + 1)){
return true;
}
}
return false;
}
if (cur.children[ch - 'a'] == null){
return false;
}
cur = cur.children[ch - 'a'];
}
return cur.isWord;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
// TC: O(n^2)
// SC: O(n)
class TrieNode{
Map<Character, TrieNode> children = new HashMap();
boolean word = false;
public TrieNode(){
}
}
class WordDictionary {
TrieNode trie;
public WordDictionary() {
trie = new TrieNode();
}
public void addWord(String word) {
TrieNode node = trie;
for (char ch : word.toCharArray()){
if (!node.children.containsKey(ch)){
node.children.put(ch, new TrieNode());
}
node = node.children.get(ch);
}
node.word = true;
}
public boolean searchInNode(String word, TrieNode node){
for (int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
if (!node.children.containsKey(ch)){
if (ch == '.'){
for (char x : node.children.keySet()){
TrieNode child = node.children.get(x);
if (searchInNode(word.substring(i + 1), child)){
return true;
}
}
}
return false;
} else {
node = node.children.get(ch);
}
}
return node.word;
}
public boolean search(String word) {
return searchInNode(word, trie);
}
}
//TC: O(n^2)
//SC: O(n)