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.

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Implement Magic Dictionary.

class MagicDictionary {
public:
vector<string> strvec;
explicit MagicDictionary() = default; void buildDict(const vector<string>& dict) {
strvec = dict;
} bool search(const string& word) { for(auto& vs : strvec){
if(vs.size() != word.size()) continue;
int idx = -;
bool fit = true;
unordered_set<char> s;
for(int i=; i<word.size(); i++){
s.insert(word[i]);
if(idx == - && word[i] != vs[i]) {idx = i;}
else if(idx != - && word[i] != vs[i]) {fit = false; break;}
}
if(idx != - && fit && s.count(word[idx])) return true;
}
return false;
}
};

LC 676. Implement Magic Dictionary的更多相关文章

  1. Week6 - 676.Implement Magic Dictionary

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

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

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

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

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

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

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

  5. 676. Implement Magic Dictionary

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

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

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

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

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

随机推荐

  1. MySQL踩坑及MySQL解压版安装

    MySQL默认当前时间: MySQL5.5版本以下是不支持:datetime default now() 的,只能写成 timestamp default now() ; 而MySQL5.6以上是支持 ...

  2. java_day04_数组

    chap04目标:数组---------------------------------------------- 1.概述 数组是一组数据的集合,数组中的每个数据被称为元素.在java中,数组也是对 ...

  3. dhcpd.conf配置文件几例

    例1   ddns-update-style interim; ignore client-updates; subnet 192.168.222.0 netmask 255.255.255.0 { ...

  4. 逆向破解 H.Koenig 遥控器 Part 1

    逆向破解 H.Koenig 遥控器(Part 1)   最近我正在尝试一研究些自动吸尘器机器人.iRobot公司的Roomba貌似是该领域的领导者,但是作为实验来讲的话这些东西真是太昂贵了,我也找不到 ...

  5. 亲测,将自己的项目部署到Github下

    转载内容,其实就是为了方便自己不用再去百度 感谢这位前辈 链接

  6. hadoop fs –stat 命令

    当向HDFS上写文件时,可以通过设置dfs.blocksize配置项来设置文件的block size,这导致HDFS上不同文件的block size是不同的.有时候我们需要知道HDFS上某个文件的bl ...

  7. python操作hive 安装和测试

    方法一:使用pyhive库 如上图所示我们需要四个外部包 中间遇到很多报错.我都一一解决了 1.Connection Issue: thrift.transport.TTransport.TTrans ...

  8. Python入门-2编程基本概念:03引用的本质-栈内存和堆内存-内存示意图

    引用 在Python中,变量也称为:对象的引用.因为,变量存储的就是对象的地址. 变量通过地址引用了“对象”. 变量位于:栈内存(压栈出栈等细节,后续再介绍). 对象位于:堆内存. Python是动态 ...

  9. [Python之路] 日志操作

    使用logging模块来写日志 日志直接输出到准备输出 import logging logging.basicConfig(level=logging.WARNING, format="% ...

  10. yii框架学习(MVC)

    路由:两种方式,第一种是默认方式访问,假设配置了虚拟主机,那么localhost/web/index.php?r=admin/index    访问的是controllers目录下的admin控制器里 ...