# Hard-题目33：97. Interleaving String

85人阅读 评论(0)

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%

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：64615次
• 积分：3288
• 等级：
• 排名：第10725名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：8条
文章分类
阅读排行
最新评论