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.

只需检测和要搜索单词长度一样的单词即可,所以我们用的数据结构就是根据单词的长度来分,把长度相同相同的单词放到一起,这样就可以减少搜索量。那么对于和要搜索单词进行比较的单词,由于已经保证了长度相等,我们直接进行逐个字符比较即可,用cnt表示不同字符的个数,初始化为0。如果当前遍历到的字符相等,则continue;如果当前遍历到的字符不相同,并且此时cnt已经为1了,则break,否则cnt就自增1。退出循环后,我们检测是否所有字符都比较完了且cnt为1,是的话则返回true,否则就是跟下一个词比较。如果所有词都比较完了,则返回false,参见代码如下:

class MagicDictionary {

    private HashMap<Integer, List<String>> map;

    /** Initialize your data structure here. */
public MagicDictionary() {
map = new HashMap<Integer, List<String>>();
} /** Build a dictionary through a list of words */
public void buildDict(String[] dict) {
for(String str : dict){
int len = str.length();
if(map.containsKey(len)){
map.get(len).add(str);
}
else{
List<String> list = new ArrayList<>();
list.add(str);
map.put(len, list);
}
}
} /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {
if(word == null || word.length() == 0){
return false;
}
int len = word.length();
if(map.containsKey(len)){
List<String> list = map.get(len);
for(String str : list){
if(hasExactOneDifference (str, word)){
return true;
}
}
}
return false;
} private boolean hasExactOneDifference(String str1, String str2){
int cnt = 0;
for(int i = 0; i< str1.length(); i++){
if(str1.charAt(i) != str2.charAt(i)){
if(cnt == 1){
return false;
}
else{
cnt++;
}
}
}
if(cnt == 1){
return true;
}
return false; }
} /**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* boolean param_2 = obj.search(word);
*/

LeetCode - Implement Magic Dictionary的更多相关文章

  1. [LeetCode] Implement Magic Dictionary 实现神奇字典

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  2. LC 676. Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  3. Week6 - 676.Implement Magic Dictionary

    Week6 - 676.Implement Magic Dictionary Implement a magic directory with buildDict, and search method ...

  4. LeetCode 676. Implement Magic Dictionary实现一个魔法字典 (C++/Java)

    题目: Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll ...

  5. [LeetCode] 676. Implement Magic Dictionary 实现神奇字典

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  6. 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...

  7. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  8. 676. Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  9. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

随机推荐

  1. 详细介绍Ubuntu 16.04系统环境安装Docker CE容器的过程

    由于项目的需要,我们在很多软件配置环境中需要用到Docker容器,这个时候我们可以用自己的VPS主机搭建.在这篇文章中,笔者将会利用Ubuntu 16.04系统环境安装Docker CE容器的过程.如 ...

  2. 使用机房的网线 连接到自己的电脑 解决Internet没有访问权限问题

    在机房把机子上的网线拔了,插在自己的笔记本上.发现并不能用,能识别Internet,但是没有访问权限. 解决办法: 去查看机房的机子的IP地址和DNS地址,就是那根网线原本连接的那台机.(网线先别拔出 ...

  3. HTML DOM 的nodeType属性

    在HTML DOM中每一部分都是节点: HTML元素是元素节点 HTML中属性是属性节点 文本是文本节点 注释是注释节点 这时我们要给它区分开我们就可以使用HTML DOM的nodeType属性 no ...

  4. Spring事务操作介绍

    Spring的特色之一,简单而强大的事务管理功能,包括编程式事务和声明式事务. 1. Spring中涉及到事务管理的API有100多个,核心的只有三个: TransactionDefinition.P ...

  5. java算法01 - 链表

    1.链表 在Java中实现链表,每个节点都有一个值,然后把它链接到下一个节点.下面来看一下节点的实现 class Node<E> { private E e; private Node&l ...

  6. JDK 1.8 -> 1.7

    电脑刚开始装的是1.8 version, 然后又需要用到1.7. 所以就要把1.8 降为1.7, 网上有很多说把1.8删掉,这种做法我是不建议的,那么要用的时候呢?又得装回来多蛋疼.. JDK 1.8 ...

  7. case 函数的简单使用记录下

    Case有2中格式:简单Case函数和Case搜索函数. 简单函数:case sex when '1' then '男' when '2' then ‘女’ else  '其它' end;(sex是列 ...

  8. 使goroutine同步的方法总结

    前言: 在前面并发性能对比的文章中,我们可以看到Golang处理大并发的能力十分强劲,而且开发也特别方便,只需要用go关键字即可开启一个新的协程. 但当多个goroutine同时进行处理的时候,就会遇 ...

  9. json_encode 的局限 , 使用自定义的函数 .returnJson.

    $arr = array("liming", "tom", "green"); $arr2 = array( 1 => "l ...

  10. 安装linux虚拟机配置静态ip(桥接模式)

    1.centOs7.VMware Workstation14 2.常规新建虚拟机操作后,来到选择连接模式: 这里选择桥接模式,复制物理网络连接状态(就是把实际的主机网卡信息拷贝一份,让虚拟机也有一份和 ...