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 的算法 待续。

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

时间: 2024-07-31 21:46:20

211 Add and Search Word - Data structure design--- back tracking, map, set 待续 trie的相关文章

【LeetCode】211. Add and Search Word - Data structure design

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 

LeetCode 211. Add and Search Word - Data structure design(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

[leedcode 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

(*medium)LeetCode 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

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: 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 le

[leetcode trie]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

Java for LeetCode 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.

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

[LC] 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

211. Add and Search Word - Data structure design

就是trie 1 public class WordDictionary { 2 public class TrieNode { 3 public TrieNode[] child; 4 public char curChar; 5 public boolean isLeaf; 6 7 public TrieNode() { 8 child = new TrieNode[26]; 9 isLeaf = false; 10 } 11 } 12 13 TrieNode root; 14 15 pub