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

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

LeetCode 211. 添加与搜索单词 - 数据结构设计_第1张图片

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

class WordDictionary {

    // Trie 10:50 11:03

    class Trie {
        Map 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);
 */

 

 

 

你可能感兴趣的:(LeetCode,Trie,字典树)