# 交错字符串

<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>	<li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li>	<li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li>	<li><code>|n - m| <= 1</code></li>	<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>	<li><code>0 <= s1.length, s2.length <= 100</code></li>	<li><code>0 <= s3.length <= 200</code></li>	<li><code>s1</code>、<code>s2</code>、和 <code>s3</code> 都由小写英文字母组成</li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isInterleave(char *s1, char *s2, char *s3)
{
	int i, j;
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	int len3 = strlen(s3);
	if (len1 + len2 != len3)
	{
		return false;
	}
	bool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool));
	bool **dp = malloc((len1 + 1) * sizeof(bool *));
	for (i = 0; i < len1 + 1; i++)
	{
		dp[i] = &table[i * (len2 + 1)];
	}
	dp[0][0] = true;
	for (i = 1; i < len1 + 1; i++)
	{
		dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];
	}
	for (i = 1; i < len2 + 1; i++)
	{
		____________________;
	}
	for (i = 1; i < len1 + 1; i++)
	{
		for (j = 1; j < len2 + 1; j++)
		{
			bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];
			bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];
			dp[i][j] = up || left;
		}
	}
	return dp[len1][len2];
}
int main(int argc, char **argv)
{
	if (argc != 4)
	{
		fprintf(stderr, "Usage: ./test s1 s2 s3\n");
		exit(-1);
	}
	printf("%s\n", isInterleave(argv[1], argv[2], argv[3]) ? "true" : "false");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isInterleave(char *s1, char *s2, char *s3)
{
	int i, j;
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	int len3 = strlen(s3);
	if (len1 + len2 != len3)
	{
		return false;
	}
	bool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool));
	bool **dp = malloc((len1 + 1) * sizeof(bool *));
	for (i = 0; i < len1 + 1; i++)
	{
		dp[i] = &table[i * (len2 + 1)];
	}
	dp[0][0] = true;
	for (i = 1; i < len1 + 1; i++)
	{
		dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];
	}
	for (i = 1; i < len2 + 1; i++)
	{
		dp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1];
	}
	for (i = 1; i < len1 + 1; i++)
	{
		for (j = 1; j < len2 + 1; j++)
		{
			bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];
			bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];
			dp[i][j] = up || left;
		}
	}
	return dp[len1][len2];
}
int main(int argc, char **argv)
{
	if (argc != 4)
	{
		fprintf(stderr, "Usage: ./test s1 s2 s3\n");
		exit(-1);
	}
	printf("%s\n", isInterleave(argv[1], argv[2], argv[3]) ? "true" : "false");
	return 0;
}
```

## 答案

```cpp
dp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1];
```

## 选项

### A

```cpp
dp[0][i] = dp[0][i + 1] && s2[i - 1] == s3[i - 1];
```

### B

```cpp
dp[0][i] = dp[0][i - 1] && s2[i + 1] == s3[i + 1];
```

### C

```cpp
dp[0][i] = dp[0][i - 1];
```