211. Add and Search Word - Data structure design
by Botao Xiao
Question
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 letters a-z or .. 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..") -> true
Thinking:
- Method:DFS, TrieTree
class WordDictionary {
TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
int len = word.length();
TrieNode temp = root;
for(int i = 0; i < len; i++){
int c = word.charAt(i) - 'a';
if(temp.child[c] == null)
temp.child[c] = new TrieNode();
temp = temp.child[c];
}
temp.isLeaf = true;
}
/** 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 search(word, -1, root);
}
private boolean search(String word, int i, TrieNode node){
if(node == null) return false;
if(i == word.length() - 1) return node.isLeaf;
if(word.charAt(++i) != '.'){
return search(word, i, node.child[word.charAt(i) - 'a']);
}else{
for(int p = 0; p < 26; p++){
if(search(word, i, node.child[p])){
if(node.child[p] == null) continue;
return true;
}
}
return false;
}
}
private static class TrieNode{
TrieNode[] child;
boolean isLeaf;
TrieNode(){
child = new TrieNode[26];
}
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
二刷
- Use trie tree as data structure to save all the words.
-
Since there ‘.’ might exist in the search words, we need to use dfs during searching. ```Java class WordDictionary { private static class TrieNode{ boolean isLeaf; TrieNode[] childs; public TrieNode(){ this.childs = new TrieNode[26]; } } private TrieNode root; /** Initialize your data structure here. */ public WordDictionary() { this.root = new TrieNode(); }
/** Adds a word into the data structure. */ public void addWord(String word) { int len = word.length(); TrieNode temp = this.root; for(int i = 0; i < len; i++){ int c = word.charAt(i) - ‘a’; if(temp.childs[c] == null){ temp.childs[c] = new TrieNode(); } temp = temp.childs[c]; } temp.isLeaf = true; }
/** 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 search(root, word, 0); }
private boolean search(TrieNode node, String s, int index){ if(index == s.length()) return node.isLeaf; char c = s.charAt(index); if(c == ‘.’){ boolean result = false; for(int i = 0; i < 26; i++){ if(node.childs[i] != null) result |= search(node.childs[i], s, index + 1); } return result; }else{ if(node.childs[c - ‘a’] == null) return false; return search(node.childs[c - ‘a’], s, index + 1); } } }
/**
- Your WordDictionary object will be instantiated and called as such:
- WordDictionary obj = new WordDictionary();
- obj.addWord(word);
- boolean param_2 = obj.search(word); */ ```
Subscribe via RSS