{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "ba9fc2ac3cc240ed95c9d30953f8166f",
  "keywords": "字符串,动态规划",
  "title": "交错字符串",
  "desc": [
    {
      "content": "\n<p>给定三个字符串 <code>s1</code>、<code>s2</code>、<code>s3</code>，请你帮忙验证 <code>s3</code> 是否是由 <code>s1</code> 和 <code>s2</code><em> </em><strong>交错 </strong>组成的。</p><p>两个字符串 <code>s</code> 和 <code>t</code> <strong>交错</strong> 的定义与过程如下，其中每个字符串都会被分割成若干 <strong>非空</strong> 子字符串：</p><ul>\t<li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li>\t<li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li>\t<li><code>|n - m| <= 1</code></li>\t<li><strong>交错</strong> 是 <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> 或者 <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code></li></ul><p><strong>提示：</strong><code>a + b</code> 意味着字符串 <code>a</code> 和 <code>b</code> 连接。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0097.Interleaving%20String/images/interleave.jpg\" style=\"width: 561px; height: 203px;\" /><pre><strong>输入：</strong>s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbcbcac\"<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbbaccc\"<strong><br />输出：</strong>false</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s1 = \"\", s2 = \"\", s3 = \"\"<strong><br />输出：</strong>true</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= s1.length, s2.length <= 100</code></li>\t<li><code>0 <= s3.length <= 200</code></li>\t<li><code>s1</code>、<code>s2</code>、和 <code>s3</code> 都由小写英文字母组成</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef isInterleave(self, s1, s2, s3):\n\t\t\"\"\"\n\t\t:type s1: str\n\t\t:type s2: str\n\t\t:type s3: str\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tif len(s1) + len(s2) != len(s3):\n\t\t\treturn False\n\t\tqueue = [(0, 0), (-1, -1)]\n\t\tvisited = set()\n\t\tisSuccess = False\n\t\tindex = 0\n\t\twhile len(queue) != 1 or queue[0][0] != -1:\n\t\t\tp = queue.pop(0)\n\t\t\tif p[0] == len(s1) and p[1] == len(s2):\n\t\t\t\treturn True\n\t\t\tif p[0] == -1:\n\t\t\t\tqueue.append(p)\n\t\t\t\tindex += 1\n\t\t\t\tcontinue\n\t\t\tif p in visited:\n\t\t\t\tcontinue\n\t\t\tvisited.add(p)\n\t\t\tif p[0] < len(s1):\n\t\t\t\tif s1[p[0]] == s3[index]:\n\t\t\t\t\tqueue.append((p[0] + 1, p[1]))\n\t\t\tif p[1] < len(s2):\n\t\t\t\tif s2[p[1]] == s3[index]:\n\t\t\t\t\tqueue.append((p[0], p[1] + 1))\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isInterleave(s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbcbcac\"))",
    "language": "python"
  },
  "node_id": "dailycode-026eee6137c34799ac0a1fe8e3cefc1a",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471101"
}