Week6 - 676.Implement Magic Dictionary
Week6 - 676.Implement Magic Dictionary
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.
my solution:
#include<string>
#include<vector>
using namespace std;
class MagicDictionary {
public:
/** Initialize your data structure here. */
vector<string> dictionary;
MagicDictionary() {
}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
dictionary = dict;
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
for (size_t i = 0; i < dictionary.size(); i++) {
if (word.size() == dictionary[i].size() ) {
int count = 0;
for (size_t j = 0; j < word.size(); j++) {
if (word[j] != dictionary[i][j]) count++;
if (count > 1) break;
}
if (count == 1) return true;
}
}
return false;
}
};
Week6 - 676.Implement Magic Dictionary的更多相关文章
- LC 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- LeetCode 676. Implement Magic Dictionary实现一个魔法字典 (C++/Java)
题目: Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll ...
- [LeetCode] 676. Implement Magic Dictionary 实现神奇字典
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...
- 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- [LeetCode] Implement Magic Dictionary 实现神奇字典
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- LeetCode - Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- [leetcode-676-Implement Magic Dictionary]
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
随机推荐
- 网络爬虫之JSOUP
JSOUP中文文档:http://www.open-open.com/jsoup/推荐博客:http://www.cnblogs.com/jycboy/p/jsoupdoc.html 从一个URL加载 ...
- 解决solr无法加core
提示缺少配置文件:Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find ...
- DRF框架之视图类
前后端分离的项目 >: pip3 install djangorestframework 一.视图类传递参数给序列化类 视图层:views.py 需求: (1)在视图类中实列化对象是,可以设 ...
- ntsysv - 用于配置运行级别的简单接口
总览 SYNOPSIS ntsysv [--back] [--level <levels>] 描述 DESCRIPTION ntsysv 是个用于配置运行级别服务(也可通过 chkconf ...
- Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration
https://www.cnblogs.com/EasonJim/p/7546136.html 错误如下: ERROR 31473 --- [ main] o.s.b.d.LoggingFailure ...
- 将Docker主机数据挂在到容器中
dcoker 提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts, tmpfs.volumes: Docker管理宿主机文件系统的一部分(/var/lib/docke ...
- grep 文本过滤
1.命令功能 grep, egrep, fgrep - print lines matching a pattern 根据匹配模式空间(正则表达式)打印结果行. 2.语法格式 grep [opti ...
- oracle汇编01
1: / define numeric label "1"one: / define symbolic label "one"/ ... assembler c ...
- Consul学习研究
参考资料 Consul 集群部署
- Linux服务器调优
Linux内核参数 http://space.itpub.net/17283404/viewspace-694350 net.ipv4.tcp_syncookies = 表示开启SYN Cookies ...