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. XML基础介绍【一】

    XML基础介绍[一] 1.XML简介(Extensible Markup Language)[可扩展标记语言] XML全称为Extensible Markup Language, 意思是可扩展的标记语 ...

  2. 建立一个可以不停地接收客户端新的连接,但不能处理复杂的业务的C/S网络程序

    在Windows平台上主要有两个版本的Socket Api函数:WinSock 1.1和WinSock 2.2 , 2.2版本默认兼容1.1版本,1.1 winsock.h wsock32.lib w ...

  3. C# NET 微信临时素材上传

    最近在做这个,一开始也是不明白为什么给个URL带着两个参数就直接上传了,网上看了很多都是PHP,但是PHP没看过是不会 的 所以就一直在找网上什么Demo之类的讲解,最后还是不错找到了一个比较好理解的 ...

  4. 23_2spring的常用注解

    1.基于注解的IOC配置 1.1导入jar包 <?xml version="1.0" encoding="UTF-8"?> <project ...

  5. maven的配置和eclipse maven插件安装

    1.下载maven:http://maven.apache.org/download.cgi 2. 配置环境变量: 3. 修改maven文件夹下bin/conf/settings.xml:maven仓 ...

  6. CSS如何水平垂直居中?

    CSS如何水平垂直居中? 1.CSS如何实现水平居中? margin: 0 auto 2.CSS如何实现水平垂直居中? 首先设置一个div元素,设置背景颜色以便看出变化.代码如下: <!DOCT ...

  7. Java类的反射

    一.类对象与反射 先来简单介绍一下反射,反射使得程序员能够更加的了解一个类,包括获得构造方法.成员方法.成员域包括注解等. 1.访问构造方法 访问构造方法有四种方式, getDeclaredConst ...

  8. python中open与with open的区别

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的.在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开 ...

  9. Vue 实现 登陆后打开主页面(登陆组件 + 主页面组件)

    本次演示,项目所需iview,router 首先 在 views 目录 新建 两个 组件 ( login.vue ,index.vue ) login.vue <template> < ...

  10. hdu 6039 Gear Up

    题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6039 (2017 Multi-University Training Contest 1 1007) ...