[LeetCode] 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.

题意及分析:设计一个支持插入和查找的数据结构,插入的字符串只包含a-z的小写字母,查找的字符串还可包含‘.’(代表任何一个字母)。这道题可以其实就是要求一个字典树,每个树的节点有26个子节点,有一个布尔类型判断从根节点到该点组成的字符串是否是一个字母。查询时如果遇到‘.’就遍历查询当前节点的所有子节点,如果有一个符合便能查找到,否则查找不到。

代码:

class TrieNode{
    public char word;       //字母
    public boolean isWord;  //判断从根节点到该点是否是一个完整的单词
    public TrieNode[] childrens = new TrieNode[26];    //子节点
    public TrieNode (){}
    public TrieNode( char word0){
        TrieNode trieNode = new TrieNode();
        trieNode.word = word0;
    }
}

public class WordDictionary {

    TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
        root.word = ' ';
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode node = root;
        for(int i=0;i<word.length();){
            while(i<word.length()&&node.childrens[word.charAt(i)-'a']!=null){
                node =node.childrens[word.charAt(i)-'a'];
                i++;
            }
            while (i<word.length()){
                node.childrens[word.charAt(i)-'a'] = new TrieNode(word.charAt(i));
                node = node.childrens[word.charAt(i)-'a'];
                i++;
            }
        }
        node.isWord = true;
    }

    /** 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) {
        TrieNode node = root;
        return searchRes(0,word,node);
    }

    private boolean searchRes(int start,String word,TrieNode node) {
        if(start == word.length()){
            if(node.isWord==true) return true;
            else return false;
        }
        if(word.charAt(start)=='.'){        //如果某点为'.'
            boolean isWord = false;
            for(int i=0;i<26;i++){
                if(node.childrens[i]!=null){
                    isWord = isWord||searchRes(start+1,word,node.childrens[i]);
                }
            }
            return isWord;
        }else{
            if(node.childrens[word.charAt(start)-'a']!=null){
                return searchRes(start+1,word,node.childrens[word.charAt(start)-'a']);
            }else
                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);
 */
原文地址:https://www.cnblogs.com/271934Liao/p/7301195.html