leetcode-676

实现一个带有buildDict, 以及 search方法的魔法字典。

对于buildDict方法,你将被给定一串不重复的单词来构建一个字典。

对于search方法,你将被给定一个单词,并且判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

示例 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
注意:

你可以假设所有输入都是小写字母 a-z。
为了便于竞赛,测试所用的数据量很小。你可以在竞赛结束后,考虑更高效的算法。
请记住重置MagicDictionary类中声明的类变量,因为静态/类变量会在多个测试用例中保留。 请参阅这里了解更多详情。

最容易想到的,字典根据单词长度分组,根据给定单词的长度搜索map,遍历得到的list,对list中每个单词进行比较,找到只有一个字母不同的单词。

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

class MagicDictionary {

    private Map<Integer,List<String >> dictMap= new HashMap<>();
    /** Initialize your data structure here. */
    public MagicDictionary() {
        
    }
    
    /** Build a dictionary through a list of words */
    public void buildDict(String[] dict) {
        for(String word:dict){
            Integer length=word.length();
            List<String> wordList=dictMap.get(length);
            if(wordList==null){
                List<String> list=new LinkedList<>();
                list.add(word);
                dictMap.put(length,list);
            }
            else{
                wordList.add(word);
            }

        }
    }
    
    /** 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) {
        List<String> wordList=dictMap.get(word.length());
        if(wordList==null){
            return false;
        }
        for(String t:wordList){
            String[] words=word.split("");
            String[] dicts=t.split("");
            if(words.length != dicts.length){
                continue;
            }
            int i=0;
            for(int j=0,size=words.length;j<size;j++){

                if(!words[j].equals(dicts[j])){
                    if(i>0){
                        i++;
                        break;
                    }else{
                        i=i+1;
                    }
                }
            }
            if(i==1){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
          String [] dict=new String[]{"hello","leetcode"};
          MagicDictionary obj = new MagicDictionary();
          obj.buildDict(dict);
          boolean param_2 = obj.search("judge");
          System.out.println(param_2);
    }
}

另一种解法,将字典中的每个单词存入hashset,将给定单词的每个字母从a-z替换,每替换一次从hashset中搜索一次。

import java.util.HashSet;
import java.util.Set;

class MagicDictionary {

    private Set<String> wordSet=new HashSet<>();
    /** Initialize your data structure here. */
    public MagicDictionary() {
        
    }
    
    /** Build a dictionary through a list of words */
    public void buildDict(String[] dict) {
        for(String word:dict){
            for(int i=0,size=word.length();i<size;i++){
               wordSet.add(word);
            }
        }
    }
    
    /** 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) {
        for(int i=0,size=word.length();i<size;i++){
            for(int j=97;j<=122;j++){
                StringBuilder sb=new StringBuilder(word);
                String s=sb.substring(i,i+1);
                if(j==(int)(s.charAt(0))){
                    continue;
                }
                sb.replace(i,i+1,""+(char)(j));
                if(wordSet.contains(sb.toString())){
                    return true;
                }
            }

        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println((int)('a')+","+(int)('z')+(char)(97));
          String [] dict=new String[]{"hello","leetcode"};
          MagicDictionary obj = new MagicDictionary();
          obj.buildDict(dict);
          boolean param_2 = obj.search("hhllo");
          System.out.println(param_2);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值