676. Implement Magic Dictionary

Problem:
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:
  1. You may assume that all the inputs are consist of lowercase letters a-z.
  2. For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
  3. 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.

Analysis:
这道题和trie tree没什么关系,只看tag太误导人了。其实非常简答。

Solution 1:
Bucket sort and one on one compare. Bucket sort to group word by length in dict. Search within the same length. And compare char one by one, only 1 diff is allowed. 

class MagicDictionary {
    Map<Integer, List<String>> bucket;
    /** Initialize your data structure here. */
    public MagicDictionary() {
        bucket = new HashMap<>();
    }
    
    /** Build a dictionary through a list of words */
    public void buildDict(String[] dict) {
        for (String s: dict) {
            bucket.computeIfAbsent(s.length(), k -> new ArrayList<>()).add(s);
        }
    }
    
    /** 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 (!bucket.containsKey(word.length())) return false;
        for (String s: bucket.get(word.length())) {
            int count = 0;
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) != s.charAt(i)) count++;
            }
            if (count == 1) return true;
        }
        return false;
    }
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary obj = new MagicDictionary();
 * obj.buildDict(dict);
 * boolean param_2 = obj.search(word);
 */

Solution 2:
Change one char in word, then search in dict set.


class MagicDictionary { Set set; /** Initialize your data structure here. */ public MagicDictionary() { set = new HashSet<>(); } /** Build a dictionary through a list of words */ public void buildDict(String[] dict) { for (String s: dict) { set.add(s); } } /** 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) { char[] chars = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char c = chars[i]; for (char t = 'a'; t <= 'z'; t++) { if (t == c) continue; chars[i] = t; if (set.contains(new String(chars))) return true; } chars[i] = c; } return false; } }

评论

此博客中的热门博文

663. Equal Tree Partition

776. Split BST

426. Convert Binary Search Tree to Sored Doubly Linked List