Leetcode211. Add and Search Word - Data structure design

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.

題目大意就是實現字典樹的查找,多了一個 “.”的匹配


class WordDictionaryNode {

    public WordDictionaryNode[] children;

    public char data;

    public int freq;

    public WordDictionaryNode() {
        children = new WordDictionaryNode[26];
        freq = 0;
    }

}

public class WordDictionary{

    private WordDictionaryNode root;

    public WordDictionary(){
        root=new WordDictionaryNode();
    }
    public void addWord(String word){
        if(word.length()==0){
            return;
        }
        addWord(root,word.toLowerCase().toCharArray(),0);
    }
    private void addWord(WordDictionaryNode rootNode,char[]charArray,int index){

        int k=charArray[index]-'a';
        if(k<0||k>25){
            throw new RuntimeException("charArray[index] is not a alphabet!");
        }
        if(rootNode.children[k]==null){
            rootNode.children[k]=new WordDictionaryNode();
            rootNode.children[k].data=charArray[index];
        }

        if(index==charArray.length-1){
            rootNode.children[k].freq++;
            return;
        }else{
            addWord(rootNode.children[k],charArray,index+1);
        }

    }
    public boolean search(String word){
        if(word.length()==0){
            return false;
        }
        return search(root, word.toCharArray(), 0);
    }
    public boolean search(WordDictionaryNode rootNode,char[]charArray,int index){
        int k=charArray[index]-'a';
        if(k!=-51&&(k<0||k>25)){
            throw new RuntimeException("charArray[index] is not a alphabet!");
        }
        if (k==-51){
            if (index == charArray.length - 1) {
                for (WordDictionaryNode tmp:rootNode.children){
                    if (tmp!=null&&tmp.freq>0) return true;
                }
                return false;
            }
            boolean flag = false;
            for (WordDictionaryNode tmp:rootNode.children) {
                if (tmp!=null) {
                    flag = search(tmp, charArray, index + 1);
                    if (flag) break;
                }
            }
            return flag;
        }
        else {
            if (rootNode.children[k] == null) {
                return false;
            }
            if (index == charArray.length - 1) {
                if (rootNode.children[k].freq>0) return true;
                else return false;
            }
            return search(rootNode.children[k], charArray, index + 1);
        }
    }
}//構造點
class WordDictionaryNode {

    public WordDictionaryNode[] children;

    public char data;

    public int freq;//記錄出現的頻率

    public WordDictionaryNode() {
        children = new WordDictionaryNode[26];//因爲題目中說都是小寫
        freq = 0;
    }

}

public class WordDictionary{

    private WordDictionaryNode root;

    public WordDictionary(){
        root=new WordDictionaryNode();
    }
    public void addWord(String word){
        if(word.length()==0){
            return;
        }
        addWord(root,word.toLowerCase().toCharArray(),0);
    }
    private void addWord(WordDictionaryNode rootNode,char[]charArray,int index){

        int k=charArray[index]-'a';
        if(k<0||k>25){
            throw new RuntimeException("charArray[index] is not a alphabet!");
        }
        if(rootNode.children[k]==null){
            rootNode.children[k]=new WordDictionaryNode();
            rootNode.children[k].data=charArray[index];
        }

        if(index==charArray.length-1){
            rootNode.children[k].freq++;
            return;
        }else{
            addWord(rootNode.children[k],charArray,index+1);
        }

    }
    public boolean search(String word){
        if(word.length()==0){
            return false;
        }
        return search(root, word.toCharArray(), 0);
    }
    public boolean search(WordDictionaryNode rootNode,char[]charArray,int index){
        int k=charArray[index]-'a';
        if(k!=-51&&(k<0||k>25)){//.是-51
            throw new RuntimeException("charArray[index] is not a alphabet!");
        }
        if (k==-51){
            if (index == charArray.length - 1) {
                for (WordDictionaryNode tmp:rootNode.children){
                    if (tmp!=null&&tmp.freq>0) return true;//freq>0來保證查找的單詞不是之前輸入的子串
                }
                return false;
            }
            boolean flag = false;
            for (WordDictionaryNode tmp:rootNode.children) {//當是"."的時候就把這一層都加入
                if (tmp!=null) {
                    flag = search(tmp, charArray, index + 1);
                    if (flag) break;
                }
            }
            return flag;
        }
        else {
            if (rootNode.children[k] == null) {
                return false;
            }
            if (index == charArray.length - 1) {
                if (rootNode.children[k].freq>0) return true;
                else return false;
            }
            return search(rootNode.children[k], charArray, index + 1);
        }
    }
    public static void main(String[] args) {
        WordDictionary wordDictionary = new WordDictionary();
        wordDictionary.addWord("WordDictionary");
        wordDictionary.addWord("addWord");
        wordDictionary.addWord("search");
        System.out.println(wordDictionary.search("addwor"));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章