zoukankan      html  css  js  c++  java
  • 211. Add and Search Word

    Design a data structure that supports the following two operations:

    void addWord(word)
    bool search(word)
    

    search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

    For example:

    addWord("bad")
    addWord("dad")
    addWord("mad")
    search("pad") -> false
    search("bad") -> true
    search(".ad") -> true
    search("b..") -> true
    

    Note:
    You may assume that all words are consist of lowercase letters a-z.


    用Trie树,如果碰到点(.),暴力地遍历当前TrieNode下的所有节点

    public class WordDictionary {
        class TrieNode {
            TrieNode[] children;
            int count;
            TrieNode() {
                count = 0;
                children = new TrieNode[26];
            }
        }
        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 cur = root;
            int i = 0;
            while (i < word.length()) {
                if (cur.children[word.charAt(i) - 'a'] == null) {
                    cur.children[word.charAt(i) - 'a'] = new TrieNode();
                }
                cur = cur.children[word.charAt(i) - 'a'];
                i++;
            }
            cur.count++;
        }
        
        /** 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 searchHelper(word, 0, root);
        }
        
        public boolean searchHelper(String word, int start, TrieNode node) {
            // node != null
            int i = start;
            TrieNode cur = node;
            while (i < word.length() && word.charAt(i) != '.') {
                if (cur.children[word.charAt(i) - 'a'] == null) {
                    return false;
                }
                cur = cur.children[word.charAt(i) - 'a'];
                i++;
            }
            // didn't meet '.'
            if (i == word.length()) {
                if (cur.count == 0) {
                    return false;
                } else {
                    return true;   
                }
            }
            // meet '.'
            for (int j = 0; j < 26; j++) {
                if (cur.children[j] != null && searchHelper(word, i + 1, cur.children[j])) {
                    return true;
                }
            }
            return false;
        }
    }
    
    /**
     * Your WordDictionary object will be instantiated and called as such:
     * WordDictionary obj = new WordDictionary();
     * obj.addWord(word);
     * boolean param_2 = obj.search(word);
     */
    
  • 相关阅读:
    Codeforces Round #595 (Div. 3) C题题解
    Educational Codeforces Round 83 (Rated for Div. 2)
    【算法竞赛进阶指南】Supermarket 贪心+并查集
    【算法竞赛进阶指南】字典树 The XOR Longest Path
    【算法竞赛进阶指南】字典树 The XOR Largest Pair
    【算法进阶指南】双端队列 DP+思维
    【算法进阶指南】蚯蚓 队列
    【2019 杭电多校第一场】Path 最短路+最小割
    【2017 ICPC 沈阳】G.Infinite Fraction Path 暴力优化
    【2017 CCPC 秦皇岛】A.Balloon Robot 思维
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/7223488.html
Copyright © 2011-2022 走看看