1. 程式人生 > >LeetCode - Implement Magic Dictionary

LeetCode - Implement Magic Dictionary

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.

只需檢測和要搜尋單詞長度一樣的單詞即可,所以我們用的資料結構就是根據單詞的長度來分,把長度相同相同的單詞放到一起,這樣就可以減少搜尋量。那麼對於和要搜尋單詞進行比較的單詞,由於已經保證了長度相等,我們直接進行逐個字元比較即可,用cnt表示不同字元的個數,初始化為0。如果當前遍歷到的字元相等,則continue;如果當前遍歷到的字元不相同,並且此時cnt已經為1了,則break,否則cnt就自增1。退出迴圈後,我們檢測是否所有字元都比較完了且cnt為1,是的話則返回true,否則就是跟下一個詞比較。如果所有詞都比較完了,則返回false,參見程式碼如下:

class MagicDictionary {

    private HashMap<Integer, List<String>> map;
        
    /** Initialize your data structure here. */
    public MagicDictionary() {
        map = new HashMap<Integer, List<String>>();
    }
    
    /** Build a dictionary through a list of words */
    public void buildDict(String[] dict) {
        for(String str : dict){
            int len = str.length();
            if(map.containsKey(len)){
                map.get(len).add(str);
            }
            else{
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(len, list);
            }
        }
    }
    
    /** 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;
        }
        int len = word.length();
        if(map.containsKey(len)){
            List<String> list = map.get(len);
            for(String str : list){
                if(hasExactOneDifference (str, word)){
                    return true;
                }
            }
        }  
        return false;
    }
    
    private boolean hasExactOneDifference(String str1, String str2){
        int cnt = 0;
        for(int i = 0; i< str1.length(); i++){
            if(str1.charAt(i) != str2.charAt(i)){
                if(cnt == 1){
                    return false;
                }
                else{
                    cnt++;
                }
            }
        }
        if(cnt == 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);
 */