题目:

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

最新文章

  1. [大数据之Yarn]——资源调度浅学
  2. openstack中eventlet使用
  3. Silverlight4中实现Theme的动态切换
  4. Vim编辑器
  5. ExtJs学习笔记之FormPanel组件
  6. 参考:iPhone OS 3.0中的字体列表
  7. python with语句上下文管理的两种实现方法
  8. lucene学习笔记:二,Lucene的框架
  9. C# winForm里窗体嵌套
  10. SDN基础
  11. 洛谷P4389 付公主的背包--生成函数+多项式
  12. 一些常用的meta标签
  13. 解决微信开发工具上trace无法检测到设备,一直停留在“正在搜索设备...”或者trace panel,choose device老出现device not found
  14. HTML做的网页 如何使当前页面跳转到另一页面锚点处
  15. KafKa记录
  16. Ruby语法基础(二)
  17. centos安装Django之二:pip3安装
  18. 【转】VISUAL STUDIO 2008代码指标为您节省资金
  19. Eclipse Kepler SR2 + Python 3.4 + JDK7+Pydev3.4 搭建 python 开发环境(MAC)
  20. bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形 计算机和

热门文章

  1. MYSQL主从复制--传统方式
  2. 趣谈Linux操作系统学习笔记:第二十六讲
  3. Linux 学习记录五(软件的安装升级).
  4. Python连载44-XML其他注意点
  5. PHP 高级面试题 - 如果没有 mb 系列函数,如何切割多字节字符串
  6. IT兄弟连 Java语法教程 流程控制语句 分支结构语句4
  7. rpmrebuild 下载安装
  8. js使用“toFixed( )”保留小数点后两位
  9. Oracle - 截取指定日期的alert log
  10. 数据库之MySQL与Python交互