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  */

 

posted @ 2019-11-13 12:46  Dylan_Java_NYC  阅读(259)  评论(0编辑  收藏  举报