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. beego中获取url以及参数的方式

    以下都全默认在controller下执行 获取当前请求的referer fmt.Println(this.Ctx.Request.Referer()) 输出:http://localhost:8080 ...

  2. mysql 设置服务器的MySQL允许远程访问/外网访问

    设置服务器的MySQL允许远程访问/外网访问 https://blog.csdn.net/weixin_34232363/article/details/85889037

  3. mtd介绍

    转:http://blog.csdn.net/lwj103862095/article/details/21545791 MTD,Memory Technology Device即内存技术设备 字符设 ...

  4. STM32输出比较模式

    搜索好久,各种文章良莠不齐,转载以下几篇 http://www.eeworld.com.cn/mcu/article_2016101130334.html(输出比较冻结模式) http://www.e ...

  5. 【转】QT中添加的资源文件qrc时的路径问题小结

    @2019-06-13 [小记] QT中添加的资源文件qrc时的路径问题小结

  6. zabbix 自定义Key (六)

    1.在zabbix_agent端zabbix_agentd.conf配置文件中增加自定义Key(/usr/local/zabbix_agent/etc/zabbix_agentd.conf) ### ...

  7. debian docker环境搭建

    环境(阿里): 登陆到系统: 我们主要看执行结果截图(所有命令都进行复制) 卸载旧版本: 使用 APT 安装: 这里 输入 y  然后等待执行结束 添加软件源的 GPG 密钥. 一开始我是手打的命令, ...

  8. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  9. 【转载】GAN for NLP 论文笔记

    本篇随笔为转载,原贴地址,知乎:GAN for NLP(论文笔记及解读).

  10. Android基础相关面试问题-binder面试问题详解

    Linux内核的基础知识: 进程隔离/虚拟地址空间:在操作系统中为了保护某个进程互不干扰就设计了一个叫“进程隔离”的技术,防止进程A可以操作进程B的数据.而进程隔离技术用到了虚拟地址空间,进程A的虚拟 ...