LeetCode 676. Implement Magic Dictionary实现一个魔法字典 (C++/Java)
题目:
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:
- You may assume that all the inputs are consist of lowercase letters
a-z. - For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
- 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)的更多相关文章
- [LeetCode] 676. Implement Magic Dictionary 实现神奇字典
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- Week6 - 676.Implement Magic Dictionary
Week6 - 676.Implement Magic Dictionary Implement a magic directory with buildDict, and search method ...
- LC 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...
- 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- Java实现 LeetCode 676 实现一个魔法字典(暴力)
676. 实现一个魔法字典 实现一个带有buildDict, 以及 search方法的魔法字典. 对于buildDict方法,你将被给定一串不重复的单词来构建一个字典. 对于search方法,你将被给 ...
- [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- [LeetCode] Implement Magic Dictionary 实现神奇字典
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- LeetCode - Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
随机推荐
- <Math> 29 365
29. Divide Two Integers class Solution { public int divide(int dividend, int divisor) { if(dividend ...
- 利用OD破解一个简单的C语言程序
最近在学习汇编(看的是王爽老师的<汇编语言(第三版)>),然后想尝试使用OD(Ollydbg)软件破解一个简单的C语言程序练练手. 环境: C语言编译环境:VC++6.0 系统:在Wind ...
- IT兄弟连 HTML5教程 HTML语言的语法 1
HTML是文本类型的语言,和其他任何一门语言相比,语法都是最简单的.但在编写HTML文件时,必须遵循HTML的语法规则.一个完整的HTML文件由标题.段落.列表.表格.文本,即嵌入的各种对象所组成,这 ...
- IT兄弟连 HTML5教程 HTML5的靠山 RFC、WHATWG是什么WEB的新标准
RFC是什么 RFC文档也称请求注解文档(Requests for Comments,RFC),这是用于发布Internet标准和Internet其他正式出版物的一种网络文件或工作报告,内容和Inte ...
- 【vim编辑器】文本编辑器vim
在Linux系统中一切皆文件.配置一个服务就是在修改其配置文件的参数 一.Vim编辑器 vim是我们在Linux系统中常用的文件编辑命令,也可以使用其简写vi.其边际模式有三种:命令模式,输入模式,行 ...
- Python 十大装 X 语法(二)
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手.不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了.本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并 ...
- PHP echo一个对象报语法错误,为什么?
为什么直接echo一个对象就会报语法错误,而如果这个对象实现了__toString方法后就可以直接输出呢? 原因是echo本来可以打印一个对象,而且也实现了这个接口,但是PHP对其做了个限制,只有实现 ...
- Fragment生命周期函数调用(ViewPager切换方式)
在使用ViewPager时,Google亲爹为我们提供了多种PagerAdapter.其中,与Fragment相关的是FragmentPagerAdapter和FragmentStatePagerAd ...
- python连接Oracle工具类
上代码: # -*- coding:utf-8 -*- import cx_Oracle import pandas as pd class ORACLE(object): def __init__( ...
- C#反射调用类的私有方法
void Main() { var type = typeof(StockClass); type.Dump(); var fields=type.GetFields(BindingFlags.Ins ...