• [LeetCode] 211. Add and Search Word


    Design a data structure that supports adding new words and finding if a string matches any previously added string.

    Implement the WordDictionary class:

    • WordDictionary() Initializes the object.
    • void addWord(word) Adds word to the data structure, it can be matched later.
    • bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.

    Example:

    Input
    ["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
    [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
    Output
    [null,null,null,null,false,true,true,true]
    
    Explanation
    WordDictionary wordDictionary = new WordDictionary();
    wordDictionary.addWord("bad");
    wordDictionary.addWord("dad");
    wordDictionary.addWord("mad");
    wordDictionary.search("pad"); // return False
    wordDictionary.search("bad"); // return True
    wordDictionary.search(".ad"); // return True
    wordDictionary.search("b.."); // return True

    Constraints:

    • 1 <= word.length <= 500
    • word in addWord consists lower-case English letters.
    • word in search consist of  '.' or lower-case English letters.
    • At most 50000 calls will be made to addWord and search.

    添加和查找单词 - 数据结构设计。

    请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。

    实现词典类 WordDictionary :

    WordDictionary() 初始化词典对象
    void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
    bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回  false 。word 中可能包含一些 '.' ,每个 . 都可以表示任何一个字母。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/design-add-and-search-words-data-structure
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题意是设计两个函数,其中代表通配符,什么字母都可以。这是一道典型的字典树(Trie)的题。同样的设计题有208题,几乎一样。字典树又叫做前缀树。字典树会为每个位置上的字母创建一个长度为26的数组,以判断当前位置上到底是哪个字母。

    当加入单词(addWord)的时候,遍历input单词的每个字母,如果当前位置上还未创建新的TrieNode,则创建。遍历完了记得改变flag和记录当前单词是什么。

    当查找单词(search)的时候,需要用DFS的方式递归寻找单词。遍历单词的每个字母,判断当前字母是否在字典树中出现。

    时间O(n), n是需要遍历的单词的长度

    空间O(num of TrieNode * 26) = O(num of words * word.length() * 26)

    Java实现

     1 class WordDictionary {
     2     class Node {
     3         Node[] children;
     4         boolean isEnd;
     5         String word;
     6 
     7         public Node() {
     8             children = new Node[26];
     9             isEnd = false;
    10             word = "";
    11         }
    12     }
    13 
    14     Node root;
    15 
    16     /** Initialize your data structure here. */
    17     public WordDictionary() {
    18         root = new Node();
    19     }
    20     
    21     public void addWord(String word) {
    22         Node node = root;
    23         for (int i = 0; i < word.length(); i++) {
    24             int j = word.charAt(i) - 'a';
    25             if (node.children[j] == null) {
    26                 node.children[j] = new Node();
    27             }
    28             node = node.children[j];
    29         }
    30         node.isEnd = true;
    31         node.word = word;
    32     }
    33     
    34     public boolean search(String word) {
    35         return helper(word, root, 0);
    36     }
    37 
    38     private boolean helper(String word, Node node, int index) {
    39         if (index == word.length()) {
    40             return node.isEnd;
    41         }
    42         if (word.charAt(index) == '.') {
    43             for (Node temp : node.children) {
    44                 if (temp != null && helper(word, temp, index + 1)) {
    45                     return true;
    46                 }
    47             }
    48             return false;
    49         } else {
    50             int j = word.charAt(index) - 'a';
    51             Node temp = node.children[j];
    52             return temp != null && helper(word, temp, index + 1);
    53         }
    54     }
    55 }
    56 
    57 /**
    58  * Your WordDictionary object will be instantiated and called as such:
    59  * WordDictionary obj = new WordDictionary();
    60  * obj.addWord(word);
    61  * boolean param_2 = obj.search(word);
    62  */

    LeetCode 题目总结

  • 相关阅读:
    用sql合并列,两句话合为一句
    微信公众平台中添加qq在线聊天代码
    我被7岁4岁的小姐妹狠狠教训了一番
    Yahoo News Digest(雅虎新闻摘要)APP的推出,未来的seo界又要受伤了
    新浪微博@别人字母不分大小写(微博昵称不分大小写)
    最近打算体验一下discuz,有不错的结构化数据插件
    使用css3来实现边框圆角效果
    用WP_Query自定义WordPress 主循环
    关闭火车头dedecms发布模块自动关键词,解决火车头发布dedecms文章关键词过多问题
    dedecms发布文章时多个Tag间分割逗号自动变成英文逗号
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12460148.html
Copyright © 2020-2023  润新知