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运用runtime全局修改UILabel的默认字体

    iOS运用runtime全局修改UILabel的默认字体 一.需求背景介绍 在项目比较成熟的基础上,遇到了这样一个需求,应用中需要引入新的字体,需要更换所有Label的默认字体,但是同时,对于一些特殊 ...

  2. vue项目出现空格警告的原因及其解决办法

    原因: 因为你的Webpack 配置中大概是使用了 eslint-loader,这是用来规范代码风格的,在多人协作或大项目中推荐使用,不想要则可以在 webpack.config.js 中去掉.esl ...

  3. springboot-自定义起步依赖

    自定义起步依赖步骤: 1.  添加configuration注解文件 -          指定什么情况下加载配置 -          使用enableconfigurationProperties ...

  4. Linux进程地址空间 && 进程内存布局[转]

    一 进程空间分布概述       对于一个进程,其空间分布如下图所示: 程序段(Text):程序代码在内存中的映射,存放函数体的二进制代码. 初始化过的数据(Data):在程序运行初已经对变量进行初始 ...

  5. 树莓派3B+学习笔记:3、启用root账户

    1.打开终端,输入 sudo passwd root 输入两次密码后设置root账户密码: 2.输入 sudo passwd --unlock root 解锁root账户: 3.点击主菜单的“Shut ...

  6. 『Python基础-13』函数 Function

    这篇笔记记录的知识点: 函数的基本概念 自定义函数 函数的几种参数 编程的三种方式: 1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程编程,万事皆过程,def定义过 ...

  7. ubuntu 防止软件包自动更新

    阻止软件包升级 有两种方法阻止软件包升级,使用dpkg,或者在Woody中使用APT. 使用dpkg,首先导出软件包选择列表: dpkg --get-selections \* > select ...

  8. 清华大学《C++语言程序设计进阶》线上课程笔记06---继承、派生、多态性

    类的继承 保持已有类的特性而构造新类的过程称为继承; 实现设计与代码的重用. 在已有类的基础上新增自己的特性而产生新类的过程称为派生 当新的问题出现,原有程序无法解决(或不能完全解决)时,需要对原有程 ...

  9. C++的特点

    C和C++ C主要是应用在在驱动层,是面向过程的编程语言,对类型的定义不是很严格.C++主要是应用与应用层,是C语言的一个加强版,可以完全兼容C语言,并且还有很多C语言不具备的特性,如,C++是一种面 ...

  10. CSS基础part2

    CSS属性操作-文本 文本颜色 <head> <style> p{ /*color:#8B5742 ;色码表*/ color: RGBA(255,0,0,0.5); /*调色, ...