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. vector,set常见方法

    vector push_back() 压入元素 size()返回元素个数 swap()交换两个向量的位置 erase()任意位置删除元素 reverse(a.begin(),a.end())翻转 se ...

  2. 分布式网上商城项目-solr搜索功能错误

    1.RuntimeException错误 java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid ...

  3. python函数名应用

    函数名的应用 函数名 的应用分类: 函数就是一个特殊的变量(可以看成一个变量来用) *函数名对应函数的内存地址 *函数名可以做为容器类数据的元素 *函数名可以作为函数的参数 *函数名可以作为函数的返回 ...

  4. .Net Core如何在程序的任意位置使用和注入服务

    最近有人问我:我该如何在Startup类之外的地方注入我的服务呢,都写在startup里看着好乱:我该如何在程序的其他地方获取我注入的服务呢: 故我写了这篇博客,文中有不对的地方欢迎指正. 一.如何在 ...

  5. 组播___IGMP

    一.基本概念: 1.协议概述: 是运行在主机和与主机直连的路由器之间,其实现的功能是双向的:一方面,主机通过IGMP通知路由器希望接收某个特定组播组的信息:另一方面,路由器通过IGMP周期性地查询局域 ...

  6. 理解golang中的channel

    channel是goroutine之间的通信机制.可以类比线程间的通信,线程间的通信有多种方式,比如线程上下文.共享内存.IPC通信.socket实现不同机器间的通信. channel用起来很简单,绑 ...

  7. 主存和cache的地址映射

    cache是一种高速缓冲寄存器,是为解决CPU和主存之间速度不匹配而采用的一项重要技术. 主存与cache的地址映射方式有全相联方式.直接方式和组相联方式三种. 直接映射(directmapping) ...

  8. 版本控制工具——Git的拓展使用

    一.使用Github 通过前面两节已经配置了SSH Key与Github上的相关设置,接下来介绍常用的使用 使用Fork克隆一份到本地仓库 之后可以在自己的仓库克隆一份到本地 git clone gi ...

  9. 注册COM组件cmd(管理员权限)

    比如,注册这个很老版本的office组件 C:\Windows\system32>regsvr32 d:\dsoframer.ocx

  10. 每天看一片代码系列(四):layzr.js,处理图片懒加载的库

    所谓图片的懒加载,即只有当图片处于或者接近于当前视窗时才开始加载图片.该库的使用方法非常简单: var layzr = new Layzr({ attr: 'data-layzr', // attr和 ...