211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)search(word) can search a literal word or a regular expression string containing only lettersa-zor.. A.means it can represent any one letter.
Example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") ->false
search("bad") ->true
search(".ad") ->true
search("b..") ->trueFB follow up: process "*"
Thoughts:
Trie Tree excercise
class WordDictionary {
private TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode node = root;
for (char c: word.toCharArray()){
if(node.children[c - 'a'] == null){
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.val = word;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return match(word, 0, root);
}
private boolean match(String word, int depth, TrieNode node){
if(depth == word.length()) return !node.val.equals("");
if(word.charAt(depth) != '.'){
return (node.children[word.charAt(depth) - 'a'] != null && match(word, depth + 1, node.children[word.charAt(depth) - 'a'] ));
}
else{
for (int i = 0; i < node.children.length; i++){
if(node.children[i] != null && match(word, depth + 1, node.children[i]))
return true;
}
}
return false;
}
}
class TrieNode{
public TrieNode [] children = new TrieNode[26];
public String val = "";
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/Python
Python: length based dictionary: psudu- O(1) - O(n)-> for char level: O(len(char in table entry))
Python: best Trie Implementation: T: O(len(total words)); S: O(len(total words))
Last updated
Was this helpful?