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 @   Dylan_Java_NYC  阅读(264)  评论(0编辑  收藏  举报
编辑推荐:
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
阅读排行:
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 想让你多爱自己一些的开源计时器
· 大模型 Token 究竟是啥:图解大模型Token
· 用99元买的服务器搭一套CI/CD系统
· 如何在 .NET 中 使用 ANTLR4
历史上的今天:
2017-11-13 LeetCode 648. Replace Words
2015-11-13 LeetCode 304. Range Sum Query 2D - Immutable
2015-11-13 LeetCode 303. Range Sum Query - Immutable
2015-11-13 LeetCode 301. Remove Invalid Parentheses
点击右上角即可分享
微信分享提示