码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-17 10:26:04      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:匹配   append   查询系统   als   sig   continue   i++   ESS   return   

题意: 设计个字典查询系统, 有 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 的算法 待续。

 

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

标签:匹配   append   查询系统   als   sig   continue   i++   ESS   return   

原文地址:https://www.cnblogs.com/keepAC/p/9972770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!