LeetCode 211. 添加与搜索单词 - 数据结构设计

211. 添加与搜索单词 - 数据结构设计

【字典树】add的时候按照正常字典树操作即可,search的时候如果遇到字母按照正常查找,遇到 ' . ' 要把所有可能的结果都遍历一遍,返回一个或的结果。

class WordDictionary {

    // Trie 10:50 11:03

    class Trie {
        Map<Character, Trie> map = new HashMap();
        boolean endOfWord = false;
    }

    Trie root = new Trie();

    public WordDictionary() {

    }
    
    public void addWord(String word) {
        Trie node = root;
        for (var i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (!node.map.containsKey(c)) {
                Trie trie = new Trie();
                node.map.put(c, trie);
            }
            node = node.map.get(c);
        }
        node.endOfWord = true;
    }

    public boolean dfs(Trie node, int i, String word, int t, int n) {
        if (t == n) {
            if (node.endOfWord) {
                return true;
            } else {
                return false;
            }
        }
        char c = word.charAt(i);
        if (c != '.') {
            if (!node.map.containsKey(c)) {
                return false;
            } else {
                return dfs(node.map.get(c), i + 1, word, t + 1, n);
            }
        } else {
            if (node.map.size() == 0) {
                return false;
            } else {
                boolean ret = false;
                for (var k: node.map.keySet()) {
                    ret = ret || dfs(node.map.get(k), i + 1, word, t + 1, n);
                }
                return ret;
            }
        }
    }
    
    public boolean search(String word) {
        return dfs(root, 0, word, 0, word.length());
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

【优化】优化一下存储结构,用数组来存,快一些

class WordDictionary {

    // Trie 1:51

    class Trie {
        Trie[] map = new Trie[26];
        boolean endOfWord = false;
    }

    Trie root = new Trie();

    public WordDictionary() {

    }
    
    public void addWord(String word) {
        Trie node = root;
        for (var i = 0; i < word.length(); i++) {
            int idx = word.charAt(i) - 'a';
            if (node.map[idx] == null) {
                Trie trie = new Trie();
                node.map[idx] = trie;
            } 
            node = node.map[idx];
        }
        node.endOfWord = true;
    }

    String word;
    int n;

    public boolean dfs(Trie node, int t) {
        if (t == n) {
            if (node.endOfWord) {
                return true;
            } else {
                return false;
            }
        }
        char c = word.charAt(t);
        if (c == '.') {
            for (var i = 0; i < 26; i++) {
                if (node.map[i] != null) {
                    if (dfs(node.map[i], t + 1)) {
                        return true;
                    }
                }
            }
            return false;
        } else {
            int idx = c - 'a';
            if (node.map[idx] == null) {
                return false;
            } else {
                return dfs(node.map[idx], t + 1);
            }
        }
    }
    
    public boolean search(String word) {
        this.word = word;
        this.n = word.length();
        return dfs(root, 0);
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值