题目:

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. FFT/NTT初探

    做了全家桶然后写了几道入门题. FFT.ref NTT.ref Luogu4238 [模板]多项式求逆 Link 套牛顿迭代完事.有一个细节问题是:这次运算多项式有几项就只赋几项的值,其他位置(次数大 ...

  2. UVA 1473 Dome of Circus

    https://cn.vjudge.net/problem/UVA-1473 题目 给出一些点,问包含这些点的最小圆锥(要求顶点在y轴,底面圆心在原点)的体积 题解 因为圆锥对称,所以可以把所有点旋转 ...

  3. 题解:A

    A (a.pas/c/cpp) [题目描述] 对于给定的一个正整数n, 判断n是否能分成若干个正整数之和 (可以重复) , 其中每个正整数都能表示成两个质数乘积. [输入描述] 第一行一个正整数 q, ...

  4. Java流程控制之顺序结构

    概述 在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说,程序的流程对运行结果有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过控制语句的执行顺序 ...

  5. 设计模式-FlyWeight(结构型模式) 针对 需要创建大量对象的情形,被共享的状态作为内部状态,不被共享的状态作为外部状态

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Flyweight.h #pragma once #include<string> class FlyWeig ...

  6. 数据可视化-matplotlib包

    pyplot官网教程https://matplotlib.org/users/pyplot_tutorial.html #导入matplotlib的pyplot模块 import matplotlib ...

  7. 第05组 Alpha冲刺(4/4)

    第05组 Alpha冲刺(4/4) 队名:天码行空 组长博客连接 作业博客连接 团队燃尽图(共享): GitHub当日代码/文档签入记录展示(共享): 组员情况: 组员1:卢欢(组长) 过去两天完成了 ...

  8. Centos7源码编译安装PHP7.2(生产环境)

    安装PHP依赖包,否则在编译的过程中可能会出现各种报错 # Centos 安装epel-release源并将系统包更新到最新版本 $ yum install epel-release-y $ yum ...

  9. Python连载48-正则表达式(中)

    一.正则的写法: . (点好) :表示任意一个字符,除了\n,比如查找所有的一个字符\. [] :匹配中括号中列举的任意字符,比如[L,Y,0], LLY, Y0, LIU \d :任意一个数字 \D ...

  10. HTML5 Canvas 为网页添加文字水印

    <!DOCTYPE html> <html> <body> <canvas id=" style="border:1px solid #d ...