题目:

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.

分析:

实现一个带有buildDict, 以及 search方法的魔法字典。

对于buildDict方法,你将被给定一串不重复的单词来构建一个字典。

对于search方法,你将被给定一个单词,并且判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

最先想到的是将每一个单词的每一个字母用另外的25个字母来替换,并放进set中,最后再在set中查询单词便可。

我们来看另一个有趣的解法。

对于一个单词,我们可以将每个字母用*号来代替存进map中,其对应的值则是替换的字母的集合,例如hello在map中存有:

*ello -> {h}

h*llo -> {e}

he*lo -> {l}

hel*o -> {l}

hell* -> {o}

当我们查询一个单词是否在魔法字典中,也将单词的每个字母用*号来替换,如果map中存在,且替换的字母不在对应set中,意味着能够查询到。

例:查询pello是否在字典中,先替换为(*ello,p),字典中有*ello,且p不在{h}中,我们应该返回true。

查询hello是否在字典中,先替换为(*ello,h),字典中有*ello,且h在{h}中,应该返回false。

如果我们将hello,pello存进字典中,再查询pello会是什么情况呢?

此时的字典中*ello->{h,p},若此时查询pello,因为p在{h,p}中,会返回false,可实际上应该要返回true的,所以我们还要加一个条件,就是或者当set中的元素大于一个的时候,意味着,*ello中的*可以替换为26个字母中的任意一个了,因为原来的hello无法通过修改一个字母来对应到hello,但有了pello的加入,hello可以通过替换第一个字母来对应到pello上。

此题还可以通过字典树来实现(后续补充)。

程序:

C++

class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
mydict.clear();
} /** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
for(string word:dict){
for(int i = ; i < word.length(); ++i){
char c = word[i];
word[i] = '*';
mydict[word].insert(c);
word[i] = c;
}
}
} /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
for(int i = ; i < word.length(); ++i){
char c = word[i];
word[i] = '*';
if(mydict.count(word)){
if (!mydict[word].count(c) || mydict[word].size() > )
return true;
}
word[i] = c;
}
return false;
}
private:
unordered_map<string, unordered_set<char>> mydict;
}; /**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dict);
* bool param_2 = obj->search(word);
*/

Java

LeetCode 676. Implement Magic Dictionary实现一个魔法字典 (C++/Java)的更多相关文章

  1. [LeetCode] 676. Implement Magic Dictionary 实现神奇字典

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  2. Week6 - 676.Implement Magic Dictionary

    Week6 - 676.Implement Magic Dictionary Implement a magic directory with buildDict, and search method ...

  3. LC 676. Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  4. 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...

  5. 676. Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  6. Java实现 LeetCode 676 实现一个魔法字典(暴力)

    676. 实现一个魔法字典 实现一个带有buildDict, 以及 search方法的魔法字典. 对于buildDict方法,你将被给定一串不重复的单词来构建一个字典. 对于search方法,你将被给 ...

  7. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  8. [LeetCode] Implement Magic Dictionary 实现神奇字典

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  9. LeetCode - Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

随机推荐

  1. LeetCode解题笔记 - 1. Two Sum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  2. 【洛谷4585】[FJOI2015] 火星商店问题(线段树分治)

    点此看题面 大致题意: 有\(n\)家店,每个商品有一个标价.每天,都可能有某家商店进货,也可能有某人去购物.一个人在购物时,会于编号在区间\([L_i,R_i]\)的商店里挑选一件进货\(d_i\) ...

  3. 利用Python几行代码批量生成验证码

    几行代码批量生成authCode 整体步骤: 1.创建图片 2.创建画笔 3.在图片上生成点 4.在图片上画线 5.在图片在画圆 6.在图片在写文本 7.在图片在生成指定字体的文本 代码奉上 #!/u ...

  4. # Spring 练习ioc 、aop

    Spring 练习 通过学习spring的基础知识,了解了Spring为了降低Java开发的复杂性,采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...

  5. 抓包工具之fiddler实战1-基本设置

    Fiddler概述 百度搜索fiddler能找到官网网站,百度软件中心也提供了下载,本人去下载了基本和官网的版本一致,但还是建议大家下载软件一定去官网进行下载. Fiddler是干什么的 在百度百科里 ...

  6. Kubernetes容器日志收集

    日志采集方式 日志从传统方式演进到容器方式的过程就不详细讲了,可以参考一下这篇文章Docker日志收集最佳实践,由于容器的漂移.自动伸缩等特性,日志收集也就必须使用新的方式来实现,Kubernetes ...

  7. GO的执行原理以及GO命令

    Go的执行原理以及Go的命令 一.Go的源码文件 Go 的源码文件分类: 如上图,分为三类: 1.命令源码文件: 声明自己属于 main 代码包.包含无参数声明和结果声明的 main 函数. 命令源码 ...

  8. qt 界面去掉系统边框

    该代码在Qt5框架编辑,使用该类时, 直接继承这个类就可以了. 实现了拖拽功能和关闭功能,如果需要放大缩小功能, 需自己实现. 1 #ifndef CUSTOMIZE_QWIDGET_H #defin ...

  9. js键盘事件以及键盘事件拦截

    一.键盘事件 onkeydown: 按下键盘时触发 onkeypress: 按下有值的键时触发 注意: onkeypress按下 Ctrl.Alt.Shift.Meta 这样无值的键,这个事件不会触发 ...

  10. 仅支持基本增删改查的学生自制php操作mysql的工具类 DB.class.php (学生笔记)

    <?php class DB{ //主机地址 var $host; //用户名 var $username; //密码 var $password; //数据库名 var $dbname; // ...