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;
}
};

最新文章

  1. 关于flume配置加载
  2. 经验总结:HTTP返回505错误小记
  3. Webwork 学习之路【07】文件上传下载
  4. 1.Android常见异常:android.view.WindowLeaked 分析以及解决办法
  5. 服务器&amp;域名那些事儿
  6. 聊聊IO多路复用之select、poll、epoll详解
  7. EasyUI实战经验总结,给有需要的人
  8. Respond.js – 让 IE6-8 支持 CSS3 Media Query
  9. Iaas-cloudstack概念
  10. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
  11. 更改git bash默认的路径
  12. Struts2之Action与配置文件
  13. asp.net C# 实现微信接口权限开发类
  14. 错误: Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for studentDao.insert
  15. Python播放、关闭音乐代码
  16. Tomcat connecttimeout sessiontimeout
  17. rhce 第十一题 挂载NFS共享
  18. ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Self-hosted debug
  19. sql server 2012的AlwaysOn高可用
  20. 一家VC支持企业的发展轨迹&mdash;&mdash;了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】

热门文章

  1. mac-svn代码管理
  2. centos6.4升级openssh7.4p1
  3. 5.Nginx的session一致性(共享)问题配置方案1
  4. shutdown immediate 持久无法关闭数据库之解决方案
  5. Summer training #7
  6. unsigned char数组赋值
  7. Web界面开发必看!Kendo UI for jQuery编辑功能指南第一弹
  8. VUE-文本-事件-属性指令
  9. Suse环境下编译linux-2.6.24内核
  10. Vue 实现 登陆后打开主页面(登陆组件 + 主页面组件)