close
211. 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.

 

用跟之前python相同的概念,用hashmap(類似 python hashtable,不過注意java的hashtable又有別的意思)來做

 依照不同word.length 為key,string為value,因為相同length一定不只有一個string,所以統一用一個ArrayList存放

 

以下是新學到的java概念

 - java的動態array 是用 ArrayList

 - HashMap(typeparameter key , typeparameter value ).  typeparameter 一定要是generic type,不能是primitive type

 - 要被共用的variable,就放在global 吧 ex.HashMap

Success:
32.85 % 216 ms 
class WordDictionary {

    HashMap<Integer,List<String>> hmap = new HashMap<Integer,List<String>>();
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        if(word != null && word.length() > 0) {        
            if(!hmap.containsKey(word.length())){
                ArrayList<String> list = new ArrayList<String>();
                list.add(word);
                hmap.put(word.length(), list);
            } else {
                hmap.get(word.length()).add(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) {
        if(hmap.isEmpty())
            return false;
        if(word == null || word.length() == 0)
            return false;
        boolean found = false;
        if(hmap.containsKey(word.length())) {
            List<String> list = hmap.get(word.length()); 
            for(int lindex=0; lindex<list.size(); lindex++) {
                String s = list.get(lindex);
                if(found == true)
                    break;
                for(int index=0; index<s.length(); index++){
                    if(word.charAt(index) != s.charAt(index) && word.charAt(index) != '.'){
                        found = false;
                        break;
                    } else
                        found = true;
                }
            }
        }
        return found;
    }
}

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

Implement a trie with insertsearch, and startsWith methods.

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

上面那題其實不是用trie 的方式解的,所以這裡就參考了一下正解,第一次使用trie的概念來解

主要重點就是

 - trienode 額外寫一個class,class 內要有指向下一個的 TrieNode children[26];

- public Trie class 內,通用的放在global 用 private保護起來

 - 注意 access string 的第n個character 可以用charAt(n),然後這裡結合 -'a' ,可以轉換成是第幾個英文數字,好在children[26]中查找是否有這個child

id = string.charAt(n) - 'a';

 - 用boolean end 標示是否是字尾

 - 因為search 和startWith 其實很像,都要一個一個比對,所以可以寫一個共同的比對,然後針對type去決定要回傳哪個結果 

Success:
48.96 % 187 ms 
class TrieNode {
    boolean end;
    TrieNode[] children;
    public TrieNode(){
        end = false;
        children = new TrieNode[26];
    }
}
public class Trie {
    private TrieNode root;
        
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode current = root;
        for(int i=0, l=word.length(); i<l; i++){
            int id = word.charAt(i) - 'a';
            if(current.children[id] == null){
                current.children[id] = new TrieNode();
                current.children[id].end = false;
            }
            current = current.children[id];
        }
        current.end = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        return search(word,1);
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        return search(prefix,2);
    }
    private boolean search(String word, int type){
        TrieNode current = root;
        for(int i=0,l=word.length(); i<l;i++){
            int id = word.charAt(i) - 'a';
            if(current.children[id] == null)    return false;
            current = current.children[id];
        }
        return type==1?current.end:true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 angledark0123 的頭像
    angledark0123

    CONY的世界

    angledark0123 發表在 痞客邦 留言(0) 人氣()