{"id":8789,"date":"2021-11-26T16:47:18","date_gmt":"2021-11-27T00:47:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8789"},"modified":"2021-11-26T17:17:55","modified_gmt":"2021-11-27T01:17:55","slug":"leetcode-30-substring-with-concatenation-of-all-words-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-30-substring-with-concatenation-of-all-words-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 30. Substring with Concatenation of All Words"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;and an array of strings&nbsp;<code>words<\/code>&nbsp;of&nbsp;<strong>the same length<\/strong>. Return&nbsp;all starting indices of substring(s) in&nbsp;<code>s<\/code>&nbsp;that is a concatenation of each word in&nbsp;<code>words<\/code>&nbsp;<strong>exactly once<\/strong>,&nbsp;<strong>in any order<\/strong>,&nbsp;and&nbsp;<strong>without any intervening characters<\/strong>.<\/p>\n\n\n\n<p>You can return the answer in&nbsp;<strong>any order<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"barfoothefoobarman\", words = [\"foo\",\"bar\"]\n<strong>Output:<\/strong> [0,9]\n<strong>Explanation:<\/strong> Substrings starting at index 0 and 9 are \"barfoo\" and \"foobar\" respectively.\nThe output order does not matter, returning [9,0] is fine too.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"wordgoodgoodgoodbestword\", words = [\"word\",\"good\",\"best\",\"word\"]\n<strong>Output:<\/strong> []\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"barfoofoobarthefoobarman\", words = [\"bar\",\"foo\",\"the\"]\n<strong>Output:<\/strong> [6,9,12]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of lower-case English letters.<\/li><li><code>1 &lt;= words.length &lt;= 5000<\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 30<\/code><\/li><li><code>words[i]<\/code>&nbsp;consists of lower-case English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable + Brute Force<\/strong><\/h2>\n\n\n\n<p>Try every index and use a hashtable to check coverage.<\/p>\n\n\n\n<p>Time complexity: O(n*m*l)<br>Space complexity: O(m*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 Solution {\npublic:\n  vector<int> findSubstring(string_view s, vector<string>& words) {\n    const int n = s.length();\n    const int m = words.size();\n    const int l = words[0].size();\n    if (m * l > n) return {};\n    unordered_map<string_view, int> freq;\n    for (const string& word : words) ++freq[word];\n    \n    vector<int> ans;\n    for (int i = 0; i <= n - m * l; ++i) {\n      unordered_map<string_view, int> seen;\n      int count = 0;\n      for (int k = 0; k < m; ++k) {\n        string_view t = s.substr(i + k * l, l);\n        auto it = freq.find(t);\n        if (it == end(freq)) break;\n        if (++seen[t] > it->second) break;\n        ++count;  \n      }\n      if (count == m) ans.push_back(i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;and an array of strings&nbsp;words&nbsp;of&nbsp;the same length. Return&nbsp;all starting indices of substring(s) in&nbsp;s&nbsp;that is a concatenation of each word in&nbsp;words&nbsp;exactly once,&nbsp;in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[217,82,4,314],"class_list":["post-8789","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hard","tag-hashtable","tag-string","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8789","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=8789"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8789\/revisions"}],"predecessor-version":[{"id":8792,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8789\/revisions\/8792"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}