676. Implement Magic Dictionary
by Botao Xiao
676. Implement Magic Dictionary
Question
Implement a magic directory with buildDict, and search methods.
For the method buildDict, you’ll be given a list of non-repetitive words to build a dictionary.
For the method search, you’ll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built.
Example 1:
Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False
Note:
- You may assume that all the inputs are consist of lowercase letters a-z.
- For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
- Please remember to RESET your class variables declared in class MagicDictionary, as static/class variables are persisted across multiple test cases. Please see here for more details.
Solution
- Method 1: Trie Tree
class MagicDictionary {
private static class Node{
boolean isLeaf;
Node[] childs;
public Node(){
this.childs = new Node[26];
}
}
private Node root;
/** Initialize your data structure here. */
public MagicDictionary() {
this.root = new Node();
}
/** Build a dictionary through a list of words */
public void buildDict(String[] dict) {
for(String s : dict){
char[] arr = s.toCharArray();
Node node = root;
for(int i = 0; i < arr.length; i++){
int c = arr[i] - 'a';
if(node.childs[c] == null){
node.childs[c] = new Node();
}
node = node.childs[c];
}
node.isLeaf = true;
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {
if(word == null || word.length() == 0) return false;
return dfs(word, 0, root, 0);
}
private boolean dfs(String word, int index, Node node, int diff){
if(diff > 1) return false;
if(index == word.length()){
if(diff == 1 && node.isLeaf){
return true;
}else{
return false;
}
}
int c = word.charAt(index) - 'a';
boolean res = false;
for(int i = 0; i < 26; i++){
if(node.childs[i] != null){
res |= dfs(word, index + 1, node.childs[i], diff + (c == i ? 0: 1));
}
}
return res;
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* boolean param_2 = obj.search(word);
*/
Subscribe via RSS