Implement a magic directory withbuildDict, andsearchmethods.
For the methodbuildDict, you'll be given a list of non-repetitive words to build a dictionary.
For the methodsearch, 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.
You may assume that all the inputs are consist of lowercase lettersa-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.
2. Implementation
(1) HashMap
classMagicDictionary {Map <String,List<int[]>> map; /** Initialize your data structure here. */publicMagicDictionary() { map =newHashMap<>(); } /** Build a dictionary through a list of words */publicvoidbuildDict(String[] dict) {for (String word : dict) {for (int i =0; i <word.length(); i++) {String key =word.substring(0, i) +word.substring(i +1);int[] info =newint[] {i,word.charAt(i)};List<int[]> list =map.getOrDefault(key,newArrayList<int[]>());list.add(info);map.put(key, list); } } } /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */publicbooleansearch(String word) {for (int i =0; i <word.length(); i++) {String key =word.substring(0, i) +word.substring(i +1);if (map.containsKey(key)) {for (int[] info :map.get(key)) {if (i == info[0] &&word.charAt(i) != info[1]) {returntrue; } } } }returnfalse; }}/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary obj = new MagicDictionary(); * obj.buildDict(dict); * boolean param_2 = obj.search(word); */
(2) Trie
classMagicDictionary {classTrieNode {char val;boolean isWord;TrieNode[] childNode;publicTrieNode(char val) {this.val= val; childNode =newTrieNode[26]; } }classTrie {TrieNode root;publicTrie() { root =newTrieNode(' '); }publicvoidinsert(String word) {if (word ==null||word.length() ==0) {return; }TrieNode curNode = root;for (char c :word.toCharArray()) {int index = c -'a';if (curNode.childNode[index] ==null) {curNode.childNode[index] =newTrieNode(c); } curNode =curNode.childNode[index]; }curNode.isWord=true; }publicbooleansearch(String word) {if (word ==null||word.length() ==0) {returnfalse; }TrieNode curNode = root;for (char c :word.toCharArray()) {int index = c -'a';if (curNode.childNode[index] ==null) {returnfalse; } curNode =curNode.childNode[index]; }returncurNode.isWord; } }Trie trie; /** Initialize your data structure here. */publicMagicDictionary() { trie =newTrie(); } /** Build a dictionary through a list of words */publicvoidbuildDict(String[] dict) {for (String word : dict) {trie.insert(word); } } /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */publicbooleansearch(String word) {char[] letters =word.toCharArray();for (int i =0; i <word.length(); i++) {char oldC = letters[i];for (char c ='a'; c <='z'; c++) {if (c == oldC) {continue; } letters[i] = c;String newWord =newString(letters);if (trie.search(newWord)) {returntrue; } } letters[i] = oldC; }returnfalse; }}/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary obj = new MagicDictionary(); * obj.buildDict(dict); * boolean param_2 = obj.search(word); */