{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "cb863b701dfd42ad9a13552cdf2bc6cb",
  "keywords": "哈希表,字符串,滑动窗口",
  "title": "串联所有单词的子串",
  "desc": [
    {
      "content": "\n<p>给定一个字符串&nbsp;<strong>s&nbsp;</strong>和一些长度相同的单词&nbsp;<strong>words。</strong>找出 <strong>s\n</strong>中恰好可以由&nbsp;<strong>words </strong>中所有单词串联形成的子串的起始位置。</p>\n<p>注意子串要与&nbsp;<strong>words </strong>中的单词完全匹配，中间不能有其他字符，但不需要考虑&nbsp;<strong>words&nbsp;</strong>中单词串联的顺序。</p>\n<p>&nbsp;</p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：  s =</strong> &quot;barfoothefoobarman&quot;,<strong>  words = </strong>[&quot;foo&quot;,&quot;bar&quot;]<strong><br />输出：</strong>[0,9]<strong><br />解释：</strong>从索引 0 和 9 开始的子串分别是 &quot;barfoo&quot; 和 &quot;foobar&quot; 。输出的顺序不重要, [9,0] 也是有效答案。</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：  s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong>  words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出：</strong>[]</pre>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef findSubstring(self, s, words):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:type words: List[str]\n\t\t:rtype: List[int]\n\t\t\"\"\"\n\t\tls = len(s)\n\t\tword_ls = len(words[0])\n\t\ttarget_dict = {}\n\t\tfor word in words:\n\t\t\ttry:\n\t\t\t\ttarget_dict[word] += 1\n\t\t\texcept KeyError:\n\t\t\t\ttarget_dict[word] = 1\n\t\tres = []\n\t\tfor start in range(ls - word_ls * len(words) + 1):\n\t\t\tcurr_dict = target_dict.copy()\n\t\t\tfor pos in range(start, start + word_ls * len(words), word_ls):\n\t\t\t\tcurr = s[pos:pos + word_ls]\n\t\t\t\ttry:\n\t\t\t\t\tcurr_dict[curr] -= 1\n\t\t\t\t\tif curr_dict[curr] < 0:\n\t\t\t\t\t\tbreak\n\t\t\t\texcept KeyError:\n\t\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tres.append(start)\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.findSubstring('wordgoodgoodgoodbestword', [\"word\", \"good\", \"best\", \"good\"]))",
    "language": "python"
  },
  "node_id": "dailycode-892f8f04d7154058aee79eb497f262e5",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470925"
}