zoukankan      html  css  js  c++  java
  • LeetCode 676. Implement Magic Dictionary

    原题链接在这里:https://leetcode.com/problems/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:

    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.

    题解:

    For each of the word in dict. Replace a char from word with "*" and use it as key, add that char to key's value set.

    e.g. "hello". -> "*ello" : h.

    When searching, repleace each char in the word with "*" and check if this key could be found in the dictionary.

    e.g. "mheeo" -> "*ello" : m.

    If it could found, check the char. If the char is not in value set or value set has it but value set also has other char, then return true.

    Note: when hm contains string, and set size > 1, that means there is another string fulfilled, return true.

    Time Complexity: buildDict, O(n*m). search, O(m). n = dict.length. m is average length of the word in the dictionary.

    Space: O(n*m).

    AC Java:

     1 class MagicDictionary {
     2     HashMap<String, HashSet<Character>> hm;
     3     
     4     /** Initialize your data structure here. */
     5     public MagicDictionary() {
     6         hm = new HashMap<>();
     7     }
     8     
     9     /** Build a dictionary through a list of words */
    10     public void buildDict(String[] dict) {
    11         for(String word : dict){
    12             for(int i = 0; i<word.length(); i++){
    13                 String sub = word.substring(0, i) + "*" + word.substring(i+1);
    14                 if(!hm.containsKey(sub)){
    15                     hm.put(sub, new HashSet<Character>());
    16                 }
    17                 
    18                 hm.get(sub).add(word.charAt(i));
    19             }
    20         }
    21     }
    22     
    23     /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
    24     public boolean search(String word) {
    25         for(int i = 0; i<word.length(); i++){
    26             String sub = word.substring(0, i) + "*" + word.substring(i+1);
    27             if(hm.containsKey(sub)){
    28                 HashSet<Character> hs = hm.get(sub);
    29                 if(!hs.contains(word.charAt(i)) || hs.size()>1){
    30                     return true;
    31                 }
    32             }
    33         }
    34         
    35         return false;
    36     }
    37 }
    38 
    39 /**
    40  * Your MagicDictionary object will be instantiated and called as such:
    41  * MagicDictionary obj = new MagicDictionary();
    42  * obj.buildDict(dict);
    43  * boolean param_2 = obj.search(word);
    44  */
  • 相关阅读:
    Unable to load the specified metadata resource
    Web开发人员速查卡
    vs 中大括号之间垂直虚线显示
    第4届华为编程大赛决赛试题解答(棋盘覆盖)
    assert()函数用法总结
    Win7安装VC++6.0已知的兼容性问题的解决方法
    VC6打开一个文件或工程的时候,会导致VC6崩溃而关闭
    浮点数取整.
    1.4 VC6.0在win7下安装的兼容性问题以及解决办法
    华为编程大赛_将字符数组内的数字排序
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11848088.html
Copyright © 2011-2022 走看看