211.Add and Search Word - Data structure design
class WordDictionary {
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) {
TrieNode curNode = root;
for(char c : word.toCharArray()){
if(curNode.children[c - 'a'] == null){
curNode.children[c - 'a'] = new TrieNode();
}
curNode = curNode.children[c - 'a'];
}
curNode.isWord = 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 match(word.toCharArray(), 0, root);
}
private boolean match(char[] chs, int k, TrieNode node){
if(k == chs.length){
return node.isWord;
}
if(chs[k] == '.'){
for(int i = 0; i < node.children.length; i++){
if(node.children[i] != null && match(chs, k + 1, node.children[i]))
return true;
}
}
else{
return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k] - 'a']);
}
return false;
}
}
class TrieNode{
public TrieNode[] children = new TrieNode[26];
public boolean isWord;
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
https://leetcode.com/problems/add-and-search-word-data-structure-design/discuss/59554
public class WordDictionary {
public class TrieNode {
public TrieNode[] children = new TrieNode[26];
public boolean isWord;
}
private TrieNode 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.isWord = 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 match(word.toCharArray(), 0, root);
}
private boolean match(char[] chs, int k, TrieNode node) {
if (k == chs.length) {
return node.isWord;
}
if (chs[k] == '.') {
for (int i = 0; i < node.children.length; i++) {
if (node.children[i] != null && match(chs, k + 1, node.children[i])) {
return true;
}
}
} else {
return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k] - 'a']);
}
return false;
}
}