0
点赞
收藏
分享

微信扫一扫

剑指offer_064 神奇的字典

禾木瞎写 2022-03-10 阅读 23

题目:

示例:

解释

提示:

代码:

class MagicDictionary {

    String[] dictionary;
    /** Initialize your data structure here. */
    public MagicDictionary() {
        dictionary = null;
    }
    
    public void buildDict(String[] dictionary) {
        this.dictionary = dictionary;
    }
    
    public boolean search(String searchWord) {
        boolean flag = false;
        for (String str : dictionary) {
            flag = getRes(str, searchWord);
            if (flag) {
                return flag;
            }
        }
        return false;
    }

    public boolean getRes(String str1, String str2) {
        if (str1.length() != str2.length() || str1.equals(str2)) {
            return false;
        }
        int count = 0;
        for (int i = 0; i < str1.length(); i++) {
            char a = str1.charAt(i);
            char b = str2.charAt(i);
            if (a != b) {
                if (++count > 1) {
                    return false;
                }
            }
        }
        return true;
    }
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary obj = new MagicDictionary();
 * obj.buildDict(dictionary);
 * boolean param_2 = obj.search(searchWord);
 */

解题思路:

这道题并没有使用前缀树,是直接比较两个字符串中有几个不同的字符。

注意:

天天开心。

参考链接:

力扣

举报

相关推荐

0 条评论