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.

思路:

用一个map记录长度为len的所有字符串,当search的时候根据字符串的长度,找到所有相同字符串的长度,然后统计map里的字符串与要

查询的字符串字符差异个数。

也可以用一个set保存所有字符串,search的时候将字符串每一位都进行a-z的替换,随时查看是否在set里面即可。

 map<int,vector<string>>mp;

 MagicDictionary()
{
mp.clear();
} /** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
if(dict.size()==)return;
for(auto s:dict)
{
mp[s.length()].push_back(s);
}
} /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
int len = word.length();
int diff =;
if(mp[len].size()==)return false; for(int i=;i<mp[len].size();i++)
{
diff==;
for(int j =;j<mp[len][i].length();j++)
{
if(word[j]!=mp[len][i][j])diff++;
}
if(diff== )return true;
}
return false;
}

[leetcode-676-Implement Magic Dictionary]的更多相关文章

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

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

  2. LeetCode 676. Implement Magic Dictionary实现一个魔法字典 (C++/Java)

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

  3. Week6 - 676.Implement Magic Dictionary

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

  4. LC 676. Implement Magic Dictionary

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

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

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

  6. 676. Implement Magic Dictionary

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

  7. [LeetCode] 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. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

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

  10. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

随机推荐

  1. ios下引用MUI后input不能输入,Android端正常

    原因是mui框架的有个css样式 *{ -webkit-user-select: none; } 其作用是禁掉用户可以选中页面中的内容. 添加以下style样式即可 input{ -webkit-us ...

  2. 浅谈React、Vue 部分异步

    React中的setState setState为什么需要异步? 无法限制何时使用异步,多次连续使用setState 防止多次渲染,异步rendering不仅仅是性能上的优化,而且这可能是react组 ...

  3. iOS 打包常见问题处理

    Cannot proceed with delivery: an existing transporter instance is currently uploading this package 原 ...

  4. angularjs中 $watch 和$on 2种监听的区别?

    1.$watch简单使用 $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEq ...

  5. 阿里云Windows远程连接出现身份验证错误,要求的函数不正确”的报错。

    最近很多阿里云用户在远程Windows Server的云服务器ECS时出现“身份验证错误,要求的函数不受支持”的报错. 这个问题解决起来非常简单,修改组策略中的一个配置就可以了. 在运行中输入gped ...

  6. ruby中的return方法及class实例方法的initialize方法

    return是函数的返回值 class Mtring def initialize(str) @name = str end def aa ary = @name.split(/:/) return ...

  7. C语言中结构体的访问方法解读

    在C语言中,对结构体的访问一般有两种常规方式:"."访问和"->"访问.那么两者有什么区别呢?对C语言有一定了解的同学应该知道,我们新建一个结构体的时候, ...

  8. 中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算

    时间换算(10分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小 ...

  9. C语言思维导图总结

  10. P2257 YY的GCD

    P2257 YY的GCD 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 k ...