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

题目:

思路:

【1】无,纯粹是要知道字典树这个东西。

代码展示:

//时间170 ms 击败 88.81%
//内存96.7 MB 击败 37.73%
class WordDictionary {
    private Trie root;

    public WordDictionary() {
        root = new Trie();
    }
    
    public void addWord(String word) {
        root.insert(word);
    }
    
    public boolean search(String word) {
        return dfs(word, 0, root);
    }

    private boolean dfs(String word, int index, Trie node) {
        if (index == word.length()) {
            return node.isEnd();
        }
        char ch = word.charAt(index);
        if (Character.isLetter(ch)) {
            int childIndex = ch - 'a';
            Trie child = node.getChildren()[childIndex];
            if (child != null && dfs(word, index + 1, child)) {
                return true;
            }
        } else {
            for (int i = 0; i < 26; i++) {
                Trie child = node.getChildren()[i];
                if (child != null && dfs(word, index + 1, child)) {
                    return true;
                }
            }
        }
        return false;
    }
}

// 字典树(前缀树)是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。
// 前缀树可以用 O(∣S∣) 的时间复杂度完成如下操作,其中 ∣S∣ 是插入字符串或查询前缀的长度:
// 1)向字典树中插入字符串 word;
// 2)查询字符串 word 是否已经插入到字典树中。
class Trie {
    private Trie[] children;
    private boolean isEnd;

    public Trie() {
        children = new Trie[26];
        isEnd = false;
    }
    
    public void insert(String word) {
        Trie node = this;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            int index = ch - 'a';
            if (node.children[index] == null) {
                node.children[index] = new Trie();
            }
            node = node.children[index];
        }
        node.isEnd = true;
    }

    public Trie[] getChildren() {
        return children;
    }

    public boolean isEnd() {
        return isEnd;
    }
}

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


 //时间165 ms 击败 95.94% 
 //内存101.4 MB 击败 15.45%
 class WordDictionary {
    // 说明了只包含小写英文字母
    private WordDictionary[] child;
    private boolean isEnd;
    public WordDictionary() {
        this.child = new WordDictionary[26];
        this.isEnd = false;
    }

    public void addWord(String word) {
        WordDictionary children = this;
        for (int i = 0; i < word.length(); i++) {
            int c = word.charAt(i) - 'a';
            if (children.child[c] == null) children.child[c] = new WordDictionary();
            children = children.child[c];
        }
        children.isEnd = true;
    }

    public boolean search(String word) {
        return search(word, 0);
    }

    private boolean search(String word, int beginIdx) {
        if (beginIdx == word.length()) {
            if (isEnd) return true;
            return false;
        }
        boolean ans;
        int c = word.charAt(beginIdx);
        if (c == '.') {
            for (WordDictionary wordDictionary : this.child) {
                if (wordDictionary != null) {
                    ans = wordDictionary.search(word, beginIdx + 1);
                    if (ans) return true;
                }
            }
        } else {
            c = c - 'a';
            if (this.child[c] == null) return false;
            return this.child[c].search(word, beginIdx + 1);
        }
        return false;
    }
}

 

posted @ 2023-07-13 12:32  忧愁的chafry  阅读(7)  评论(0编辑  收藏  举报