题目:

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. 3. java 方法入门

    一.方法定义 1. 定义格式 public static void 方法名称(){ 方法体 } 1. 方法名称:命名和变量一致,小驼峰式 2. 方法体:大括号中可以包含任意条语句 注意事项: 1. 方 ...

  2. Linux下安装redis报错信息

    redis在Linux安装报错 标签: redislinuxcentos 2017-02-24 13:46 384人阅读 评论(0) 收藏 举报  分类: Linux安装工具(2)  版权声明:本文为 ...

  3. javaee和javase的区别

    JavaEE是指Java Enterprise Edition,Java企业版,多用于企业级开发,包括web开发等等.也叫J2EE. JavaSE通常是指Java Standard Edition,J ...

  4. nginx学习(四):nginx处理web请求机制

    worker抢占机制 如下图所示,如果有一个请求,各个work进程会进行争锁.谁抢到是谁的.需要注意Nginx 所有worker进程协同工作的关键(共享内存). [accept_mutex的介绍] 当 ...

  5. python--8大排序(原理+代码)

    常用的排序方法:冒泡排序.选择排序.插入排序.快速排序.堆排序.归并排序 冒泡排序(Bubble Sort): 比较相邻的元素.如果第一个比第二个大(升序),就交换他们两个. 对每一对相邻元素作同样的 ...

  6. springboot-热部署Jrebel

    1. 场景描述 介绍下idea+springboot下的热部署插件-Jrebel,贼好用,以前用过好多种,但是总出现不稳定或者会莫名其妙的没有部署新代码. 2.解决方案 springboot自带的de ...

  7. jQuery 源码分析(十三) 数据操作模块 DOM属性 详解

    jQuery的属性操作模块总共有4个部分,本篇说一下第2个部分:DOM属性部分,用于修改DOM元素的属性的(属性和特性是不一样的,一般将property翻译为属性,attribute翻译为特性) DO ...

  8. 红黑树之 原理和算法详细介绍(阿里面试-treemap使用了红黑树) 红黑树的时间复杂度是O(lgn) 高度<=2log(n+1)1、X节点左旋-将X右边的子节点变成 父节点 2、X节点右旋-将X左边的子节点变成父节点

    红黑树插入删除 具体参考:红黑树原理以及插入.删除算法 附图例说明   (阿里的高德一直追着问) 或者插入的情况参考:红黑树原理以及插入.删除算法 附图例说明 红黑树与AVL树 红黑树 的时间复杂度 ...

  9. python爬虫:将数据保存到本地

    一.python语句存储 1.with open()语句 with open(name,mode,encoding) as file: file.write() name:包含文件名称的字符串; mo ...

  10. Java8 日期和时间API

    LocalDate.LocalTime.Instant.Duration.Period 1.1使用LocalDate和LocalTime 1.1.1LocalDate的创建方式和相关方法使用示例 @T ...