二刷211. Add and Search Word - Data structure design

讲解很棒:https://www.youtube.com/watch?v=8d_HdK0f2B4
真的是要感谢YouTube上这些分享讲解的作者,解决了我很多问题。
这个题除了search里面word可能包含通配符,其他都跟208. Implement Trie (Prefix Tree)差不多,所以我只说一下这里的find.

这里的find因为可以用b*d这样的word去搜索,所以我们要处理这种情况。其实很简单,遇到*, 我们直接跳过,然后在*这个node的孩子里去搜索b*d这个word里*后面的部分,这里就是d. 于是我们只需要判断该char== *?然后用int startIndex来标记开始找的单词substring, 遇到*就在当前节点的孩子里找剩下的substring,对应的代码是:

 if (word.charAt(index) == '.'){
        for (TrieNode temp : node.children){
            if (temp != null && find(word, temp, index + 1)){
                return true;
            }
        }
        return false;
  }

而对待不是*的部分就是正常的dfs逐层深入了,先找到char对应的孩子index j, 然后看这个孩子是否为空,为空就说明我们从来没有insert 过要搜索的这个词。不为空我们还要在当前node的孩子里继续检查后面的substring, 我们通过开始访问的char的index index 来做这件事情。并且每次find开始,记得检查index == word.length(). 如果是的话说明我们已经找到了这个词,但不能直接返回true, 还要看node.isWord. 比如我们insert的是badass, 你搜索bad, 是应该返回false的而不是true.

class WordDictionary {
    class TrieNode{
        TrieNode[] children;
        boolean isWord;
        String word;
        public TrieNode(){
            children = new TrieNode[26];
            isWord = false;
        }
    }
    TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode p = root;
        for (int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if (p.children[index] == null){
                p.children[index] = new TrieNode();
            }
            p = p.children[index];
        }
        p.isWord = true;
        p.word = word;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return find(word, root, 0);    
    }
    
    private boolean find(String word, TrieNode node, int index){
        if (index == word.length()){
            return node.isWord;
        }
        if (word.charAt(index) == '.'){
            for (TrieNode temp : node.children){
                if (temp != null && find(word, temp, index + 1)){
                    return true;
                }
            }
            return false;
        } else {
            int j = word.charAt(index) - 'a';
            TrieNode temp = node.children[j];
            return temp != null && find(word, temp, index + 1);
        }
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,775评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • 【小目标:收集与整理Github 每天Android 热门开源库】 内容 TextView 图片 Theme 主题...
    MryU93阅读 531评论 0 0
  • 办公室主任小方,不,刚刚被宣布任命为方副部长的胃癌已经晚期了,终于知道再也瞒不住,往上爬的斗志也彻底松懈下来。...
    天涯孤旅背包客阅读 265评论 0 5
  • 老树庭前新吐芽,忆当时正是无邪。瓜雕灯盏如圆月,纸剪风鸢逐晚霞。 才戏水,又攒沙,垂钩击棹弄舟斜。忽然日暮炊烟起,...
    泓颖阅读 600评论 6 7