{"id":9050,"date":"2021-12-05T19:55:03","date_gmt":"2021-12-06T03:55:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9050"},"modified":"2021-12-05T19:55:54","modified_gmt":"2021-12-06T03:55:54","slug":"leetcode-211-design-add-and-search-words-data-structure","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/data-structure\/leetcode-211-design-add-and-search-words-data-structure\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 211. Design Add and Search Words Data Structure"},"content":{"rendered":"\n<p>Design a data structure that supports adding new words and finding if a string matches any previously added string.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>WordDictionary<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>WordDictionary()<\/code>&nbsp;Initializes the object.<\/li><li><code>void addWord(word)<\/code>&nbsp;Adds&nbsp;<code>word<\/code>&nbsp;to the data structure, it can be matched later.<\/li><li><code>bool search(word)<\/code>&nbsp;Returns&nbsp;<code>true<\/code>&nbsp;if there is any string in the data structure that matches&nbsp;<code>word<\/code>&nbsp;or&nbsp;<code>false<\/code>&nbsp;otherwise.&nbsp;<code>word<\/code>&nbsp;may contain dots&nbsp;<code>'.'<\/code>&nbsp;where dots can be matched with any letter.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"WordDictionary\",\"addWord\",\"addWord\",\"addWord\",\"search\",\"search\",\"search\",\"search\"]\n[[],[\"bad\"],[\"dad\"],[\"mad\"],[\"pad\"],[\"bad\"],[\".ad\"],[\"b..\"]]\n<strong>Output<\/strong>\n[null,null,null,null,false,true,true,true]\n<strong>Explanation<\/strong> \nWordDictionary wordDictionary = new WordDictionary(); \nwordDictionary.addWord(\"bad\"); \nwordDictionary.addWord(\"dad\"); \nwordDictionary.addWord(\"mad\"); \nwordDictionary.search(\"pad\"); \/\/ return False \nwordDictionary.search(\"bad\"); \/\/ return True \nwordDictionary.search(\".ad\"); \/\/ return True \nwordDictionary.search(\"b..\"); \/\/ return True\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word.length &lt;= 500<\/code><\/li><li><code>word<\/code>&nbsp;in&nbsp;<code>addWord<\/code>&nbsp;consists lower-case English letters.<\/li><li><code>word<\/code>&nbsp;in&nbsp;<code>search<\/code>&nbsp;consist of&nbsp;&nbsp;<code>'.'<\/code>&nbsp;or lower-case English letters.<\/li><li>At most&nbsp;<code>50000<\/code>&nbsp;calls will be made to&nbsp;<code>addWord<\/code>&nbsp;and&nbsp;<code>search<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtables<\/strong><\/h2>\n\n\n\n<p>The first hashtable stores all the words, if there is no dot in the search pattern. Do a full match.<\/p>\n\n\n\n<p>There are also per length hashtable to store words of length k. And do a brute force match.<\/p>\n\n\n\n<p>Time complexity: Init: O(n*l)<br>search: best: O(l) worst: O(n*l)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass WordDictionary {\npublic:\n  \/\/ Adds a word into the data structure.\n  void addWord(string word) {\n    words.insert(word);\n    ws[word.length()].insert(word);\n  }\n\n  \/\/ Returns if the word is in the data structure. A word could\n  \/\/ contain the dot character '.' to represent any one letter.\n  bool search(string word) {\n    if (word.find(\".\") == string::npos)\n      return words.count(word);\n    \n    for (const string& w : ws[word.length()])\n      if (match(word, w)) return true;    \n\n    return false;\n  }\n    \n  bool match(const string& p, const string& w) {\n    for (int i = 0; i < p.length(); ++i) {\n      if (p[i] == '.') continue;\n      if (p[i] != w[i]) return false;\n    }\n    return true;\n  }\nprivate:\n    unordered_set<string> words;\n    unordered_map<int, unordered_set<string>> ws;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a data structure that supports adding new words and finding if a string matches any previously added string. Implement the&nbsp;WordDictionary&nbsp;class: WordDictionary()&nbsp;Initializes the object. void&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[291,82,177,4],"class_list":["post-9050","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-data-structure","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9050"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9050\/revisions"}],"predecessor-version":[{"id":9052,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9050\/revisions\/9052"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}