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 ...
随机推荐
- vue项目1-pizza点餐系统8-登陆和注册结构
<template> <!-- 设置行 --> <div class="row mt-3"> <!-- 设置列 --> <di ...
- 一、.net Core bundleconfig.json
一.bundleconfig.json [ { "outputFileName": "wwwroot/css/site.min.css", "inpu ...
- 10年前文章_iscsi initiator 设置
发现和查找 target, iscsiadm -m discovery -t sendtargets -p 192.168.1.1:3260 登录 iscsiadmin -m node -l 退出登录 ...
- Linux shell 误操作
shell脚本在日常运维中是必不可少会应用到,下面是自己亲身经历过的一件事.会了定期清除日志,编写了一个shell脚本,内容如下: [root@centos- tmp]# more remote_lo ...
- python笔记(3)---列表
lst = ["bob","jack","马云","马化腾","王健林"] del lst[1:3] ...
- 子类重用父类的功能super
# class OldboyPeople:# school = 'oldboy'# def __init__(self,name,age,gender):# self.name=name# self. ...
- Debian10+OpenMediaVault(OMV)安装
前言:测试打造NAS平台,以下是步骤. 安装Debian10 注:请下载amd64,不要下载i836平台,因为OMV外挂插件不支持I836所以不建议用i836,如只使用官方插件可以无视 安装前-安装, ...
- 【LuoguP3241】[HNOI2015] 开店
题目链接 题意 给出一棵边带权的树,多次在线询问一个点到一个区间内的点的距离和. Sol 分块过不了的 一个 trick ,都知道要算两点之间距离可以拆成到根的距离和他们的 LCA 到根的距离 ,其实 ...
- 设计模式学习笔记——Adapter 适配器模式
适配器设计模式的适应场景: 一般情况是上端固定,下端固定,下端功能不满足或跟上端不协调,使用适配器重新包一层(继承适配器接口,以满足上端需求,继承下层类,以调用方法),使下端代码能满足上端需求(欺骗, ...
- echarts曲线图
drawLeftLine(){ let drawLine = echarts.init(document.getElementById('data-left-middle-table-wrap')); ...