1. 程式人生 > >211 Add and Search Word - Data structure design--- back tracking, map, set 待續 trie

211 Add and Search Word - Data structure design--- back tracking, map, set 待續 trie

題意: 設計個字典查詢系統, 有 add 和search 兩種操作, add 是加入單詞到字典裡, search 時 可以用 點號萬用字元 ".", 點號可以匹配一個字母。

分析: 當search 時為 萬用字元時, 如果直接用back tracking產生 a-z, 比如 有7個點號, 就得生成  26^7 個組合,會TLE。 

以下是TLE的code: 

class WordDictionary {

    /** Initialize your data structure here. */
    Set<String> dic;
    
public WordDictionary() { dic = new HashSet<>(); } /** Adds a word into the data structure. */ public void addWord(String word) { dic.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) { return dfs(new StringBuilder(), word, 0); } private boolean dfs(StringBuilder curResult, String word, int index){ if(curResult.length() == word.length()){ // System.out.println(curResult.toString()); if
(dic.contains(curResult.toString())) return true; return false; } boolean success = false; char cur_ch = word.charAt(index); if(cur_ch == '.'){ for(int i=0; i<26; i++){ curResult.append((char)(i+'a')); success = success || dfs(curResult,word,index+1); curResult.setLength(curResult.length()-1); if(success) return true; } } else { curResult.append(cur_ch); success = success || dfs(curResult,word,index+1); curResult.setLength(curResult.length()-1); if(success) return true; } return success; } } /** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */

 

 改進: 用map 來存放 <長度+ List<String> > 的組合, 匹配一個單詞 首先得長度匹配,才能進一步匹配。

換成如下 演算法能beat 99%, 但如果單詞長度全部一樣, 那 變成了 n^2的演算法了。主要還是測試資料太弱了。

class WordDictionary {

    /** Initialize your data structure here. */
    Map<Integer, List<String>> dict;
    public WordDictionary() {
       dict = new HashMap<>();  
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        List<String> val = dict.getOrDefault(word.length(),new ArrayList<>()) ;
        val.add(word);
        dict.put(word.length(), val);
    }
    
    /** 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(!dict.containsKey(word.length())) return false; 
       
        List<String> list = dict.get(word.length());
        
        int i;
        for(String str: list){
           
            for(i=0; i<word.length(); i++){
                char c = word.charAt(i);
                if(c == '.') continue;
                if(c != str.charAt(i)) break;
            }
            if(i == word.length()) return true;
        }
        return false;
    }
}

 

Trie 的演算法 待續。