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  */
  • 相关阅读:
    linux入门系列8--shell编程入门
    linux入门系列7--管道符、重定向、环境变量
    linux入门系列6--软件管理之rpm和yum仓库
    linux入门系列5--新手必会的linux命令
    linux入门系列4--vi/vim编辑器
    linux入门系列3--常见的linux远程登陆管理工具
    linux入门系列2--CentOs图形界面操作及目录结构
    Linux入门系列1--环境准备及Linux安装
    曾经我也有一个做游戏的梦想,这几本游戏开发的书籍推荐给为未来的游戏工程师
    互联网公司的敏捷开发是怎么回事?这一份软件工程书单送给你!
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11848088.html
Copyright © 2011-2022 走看看