LC 676. Implement Magic Dictionary
2024-09-05 13:14:51
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:
- You may assume that all the inputs are consist of lowercase letters
a-z
. - For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
- 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;
}
};
最新文章
- 关于flume配置加载
- 经验总结:HTTP返回505错误小记
- Webwork 学习之路【07】文件上传下载
- 1.Android常见异常:android.view.WindowLeaked 分析以及解决办法
- 服务器&;域名那些事儿
- 聊聊IO多路复用之select、poll、epoll详解
- EasyUI实战经验总结,给有需要的人
- Respond.js – 让 IE6-8 支持 CSS3 Media Query
- Iaas-cloudstack概念
- ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
- 更改git bash默认的路径
- Struts2之Action与配置文件
- asp.net C# 实现微信接口权限开发类
- 错误: Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for studentDao.insert
- Python播放、关闭音乐代码
- Tomcat connecttimeout sessiontimeout
- rhce 第十一题 挂载NFS共享
- ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Self-hosted debug
- sql server 2012的AlwaysOn高可用
- 一家VC支持企业的发展轨迹&mdash;&mdash;了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】