力扣:676. 实现一个魔法字典

本文介绍了如何使用暴力搜索算法、基于数组的字典树和对象的字典树来解决字符串查找问题。通过实例展示了不同实现方式在魔法字典中的应用,并对比了它们的效率和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



/**
 * @author xnl
 * @Description:
 * @date: 2022/7/11   21:42
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        MagicDictionary3 magicDictionary = new MagicDictionary3();
        magicDictionary.buildDict(new String[]{"hello", "hallo", "leetcode"});
        System.out.println(magicDictionary.search("hello"));
    }
}

/**
 * 使用暴力
 */
class MagicDictionary {

    private String[] dictionary;
    public MagicDictionary() {

    }

    public void buildDict(String[] dictionary) {
        this.dictionary = dictionary;
    }

    public boolean search(String searchWord) {
        for (int i = 0; i < dictionary.length; i++){
            if (dictionary[i].length() != searchWord.length()){
                continue;
            }
            int diff = 0;
            int index = 0;
            while (index < searchWord.length()){
                if (dictionary[i].charAt(index) != searchWord.charAt(index) ){
                    diff++;
                }
                if (diff > 1){
                    break;
                }
                index++;
            }
            if (diff == 1){
                return true;
            }
        }
        return false;
    }
}

/**
 * 数组实现
 */
class MagicDictionary2 {

    int n = 100 * 100, m = 26, idx = 0;
    int[][] tr = new int[n][m];
    boolean[] isEnd = new boolean[n * m];

    void add(String str){
        int p = 0;
        for (int i = 0; i < str.length(); i++){
            int u = str.charAt(i) - 'a';
            if (tr[p][u] == 0){
                tr[p][u] = ++idx;
            }
            p = tr[p][u];
        }
        isEnd[p] = true;
    }

    public void buildDict(String[] dictionary) {
        for (String s : dictionary) {
            add(s);
        }
    }

    private boolean query(String s, int idx, int p, int limit){
        if (limit < 0){
            return false;
        }
        if (idx == s.length()){
            return isEnd[p] && limit == 0;
        }

        int u = s.charAt(idx) - 'a';
        for (int i = 0;  i < 26; i++){
            if (tr[p][i] == 0){
                continue;
            }
            if (query(s, idx + 1, tr[p][i], i == u ? limit : limit - 1)){
                return true;
            }
        }
        return false;
    }

    public boolean search(String s) {
        return query(s, 0, 0, 1);
    }
}


/**
 * 对象实现
 */
class MagicDictionary3 {

    class Trie{
        boolean isEnd;
        Trie[] child;

        public Trie() {
            isEnd = false;
            child = new Trie[26];
        }
    }

    private Trie root;

    public MagicDictionary3(){
        this.root = new Trie();
    }

    void add(String str){
        Trie cur = root;
        for (int i = 0; i < str.length(); i++){
            int u = str.charAt(i) - 'a';
            if (cur.child[u] == null){
                cur.child[u] = new Trie();
            }
            cur = cur.child[u];
        }
        cur.isEnd = true;
    }

    public void buildDict(String[] dictionary) {
        for (String s : dictionary) {
            add(s);
        }
    }

    private boolean query(String s, Trie node, int pos, boolean modified){
        if (pos == s.length()){
            return node.isEnd && modified;
        }

        int u = s.charAt(pos) - 'a';
        if (node.child[u] != null){
            if (query(s, node.child[u], pos + 1, modified)){
                return true;
            }
        }

        if (!modified){
            for (int i = 0; i < 26; i++){
                if (i != u && node.child[i] != null){
                    if (query(s, node.child[i], pos + 1, true)){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean search(String s) {
        return query(s, root, 0, false);
    }
}

三种解法

暴力

字典树 数组实现

字典树 对象实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值