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. python 变量定义

    变量定义 什么是变量? 在程序运行过程中,其值可以改变的量. 标识符(命令规范) 只能由数字.字母.下划线组成 不能以数字开头 不能是系统关键字 import keyword ​ # 打印关键字列表 ...

  2. 基于MySql和Sails.js的RESTful风格的api实现

    Sails.js是类似于express的node后台框架,她面向数据库的特性使得面向数据库的服务器的搭建变得特别简单快捷. 现在网上关于Sails的教程大多是基于V0.12版本的,但是现在Sails的 ...

  3. Enable directory listing on Nginx Web Server

    1:Test environment [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core ...

  4. Redis学习笔记(一)

    定义 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库. 从该定义中抽出几个关键信息,以表示Redis的特性: 存储结构:key-val ...

  5. 网页股票期货历史数据(API)

    //[和讯数据] //大商所DCE.郑商所CZCE.上期所SHFE3.中金所CFFEX //期货1分钟线http://webftcn.hermes.hexun.com/ ... I1709&d ...

  6. django的render的特殊用法

    以前都是将模板渲染好, 传输到前端, 但是现在前后端分离了, 模板渲染引擎还有用, 而且很好用. 比如在渲染一个表格的时候, 每一行都有两个操作按钮, 并且这个按钮上是有a标签的 你可以使用字符串拼接 ...

  7. opencv移植(二)

    原文:https://blog.csdn.net/Guet_Kite/article/details/78667175?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接 ...

  8. 面试乐融集团Python开发工程师有感

    这是笔者第一次面试,,乐融集团位于朝阳区朝阳公园的乐融大厦.是下午两点的笔面试,笔者是一点半到的,然后在里面等了会,开始笔试 笔试题并不是太难,就是考的比较宽,因为笔者是校招,所以笔试题出来了数据结构 ...

  9. 网络文件系统nfs在ubuntu16.04的安装

    1.搜索nfs-sudo apt-cache search nfs- 2.安装sudo apt-get install nfs-kernel-server 3.配置:/etc/exports /hom ...

  10. C语言中malloc函数的理解

    在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...