# Hard-题目33：97. Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = “aabcc”,
s2 = “dbbca”,

When s3 = “aadbbcbcac”, return true.
When s3 = “aadbbbaccc”, return false.

dp(I,j)=(dp(i-1,j)&&s1[i-1]==s3[i+j-1])||(dp(I,j-1)&&s2[j-1]==s3[i+j-1])

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int m = s1.size();
int n = s2.size();
if(m+n != s3.size())
return false;
vector<vector<bool> > path(m+1, vector<bool>(n+1, false));
for(int i = 0; i < m+1; i ++)
{
for(int j = 0; j < n+1; j ++)
{
if(i == 0 && j == 0)
// start
path[i][j] = true;
else if(i == 0)
path[i][j] = path[i][j-1] & (s2[j-1]==s3[j-1]);
else if(j == 0)
path[i][j] = path[i-1][j] & (s1[i-1]==s3[i-1]);
else
path[i][j] = (path[i][j-1] & (s2[j-1]==s3[i+j-1])) || (path[i-1][j] & (s1[i-1]==s3[i+j-1]));
}
}
return path[m][n];
}
};

12ms,8.48%,8ms,28.70%

• 本文已收录于以下专栏：

## 97 - Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s...
• hellochenlu
• 2016年03月14日 09:39
• 657

## leetcode_question_97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
• doc_sgl
• 2013年09月15日 22:08
• 6375

## LeetCode 97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
• mlweixiao
• 2014年10月09日 13:17
• 1077

## [leetcode-97]Interleaving String（java)

• zdavb
• 2015年08月11日 10:24
• 494

## 97.Interleaving String (二维动态规划，没想明白)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given...
• sjt091110317
• 2015年07月01日 15:53
• 393

## leetcode: 97. Interleaving String

QGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: ...
• JNingWei
• 2017年11月22日 10:25
• 234

## Leetcode——97. Interleaving String

• juzihongle1
• 2017年05月08日 11:45
• 115

## Leetcode 97. Interleaving String

97. Interleaving String Total Accepted: 49560 Total Submissions: 219537 Difficulty: Hard ...
• fantasiasango
• 2016年05月19日 10:04
• 167

## [hard]97. Interleaving String

97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 a...
• submit_homework
• 2016年11月08日 14:23
• 54

## Leetcode:97. Interleaving String

Total Accepted: 48816 Total Submissions: 217231 Difficulty: HardGiven s1, s2, s3, find whether s3 is...
• ofantasyx
• 2016年05月01日 13:02
• 108

举报原因： 您举报文章：Hard-题目33：97. Interleaving String 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)