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.
[Analysis]
方法一:递归(Large Judge会TLE)
(1)如果s1[0] == s3[0] 且 s2[0] == s3[0])
isInterleave(s1.substr(1, len1-1), s2, s3.substr(1, len3-1)) 或者 isInterleave(s1, s2.substr(1, len2-1), s3.substr(1, len3-1));
(2)如果s1[0] == s3[0])
isInterleave(s1.substr(1, len1-1), s2, s3.substr(1, len3-1));
(3)如果s2[0] == s3[0])
isInterleave(s1, s2.substr(1, len2-1), s3.substr(1, len3-1));
(4)否则false;
方法二:动态规划
定义bool mark[len1+1][len2+1],初始化为false,且mark[len1][len2]为true,从i=len1, j=len2, i--, j--开始更新mark数组,mark[i][j]表示使用s1[i:len1]与s2[j:len2]能否组成s3[len3-(len1-i+len2-j):len3],true为可以组成,false为不可以组成,更新mark[i][j]时:
(1)如果mark[i][j+1]=true,表明s1[i:len1]与s2[j+1:len2]能够组成s3[len3-(len1-i+len2-j-1):len3],现在考虑mark[i][j],表明在s1[i:len1]与s2[j+1:len2]的基础上加入s2[j],那么只需要判断s2[j]是否与s3[len3-(len1-i+len2-j)]是否相等,如果相等,则mark[i][j]=true,否则不更新mark[i][j]
if(j < len2 && mark[i][j+1] == true && s2[j] == s3[len3-(len1-i+len2-j)]){
mark[i][j] = true;
}
(1)同理,如果mark[i+1][j]=true,表明s1[i+1:len1]与s2[j:len2]能够组成s3[len3-(len1- i+len2-j-1):len3],现在考虑mark[i][j],表明在s1[i+1:len1]与s2[j:len2]的基础上加入s1[i],那么只需要判断s1[i]是否与s3[len3-(len1-i+len2-j)]是否相等,如果相等,则mark[i][j]=true,否则不更新mark[i][j]
if(i < len1 && mark[i+1][j] == true && s1[i] == s3[len3-(len1-i+len2-j)]){
mark[i][j] = true;
}
[Solution]
方法一:递归(Large Judge会TLE)
bool isInterleave(string s1, string s2, string s3) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
if(len3 != (len1+len2)){
return false;
}
if(len3 == 0){
return true;
}
if(s1[0] == s3[0] && s2[0] == s3[0]){
return isInterleave(s1.substr(1, len1-1), s2, s3.substr(1, len3-1)) || isInterleave(s1, s2.substr(1, len2-1), s3.substr(1, len3-1));
}
else if(s1[0] == s3[0]){
return isInterleave(s1.substr(1, len1-1), s2, s3.substr(1, len3-1));
}
else if(s2[0] == s3[0]){
return isInterleave(s1, s2.substr(1, len2-1), s3.substr(1, len3-1));
}
else{
return false;
}
}
方法二:动态规划
class Solution {
public:
// interleave
bool isInterleave(string s1, string s2, string s3){
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
// invalid strings
if(len3 != len1+len2){
return false;
}
// emtpy string
else if(len3 == 0){
return true;
}
// non-empty strings
bool mark[len1+1][len2+1];
memset(mark, false, sizeof(mark));
mark[len1][len2] = true;
// dynamic search
for(int i = len1; i >= 0; --i){
for(int j = len2; j >= 0; --j){
if(i == len1 && j == len2){
continue;
}
// s1[i:len1] and s2[j+1:len2] can interlave s3[len3-(len1-i+len2-j-1):len3]
if(j < len2 && mark[i][j+1] == true && s2[j] == s3[len3-(len1-i+len2-j)]){
mark[i][j] = true;
}
// s1[i+1:len1] and s2[j:len2] can interlave s3[len3-(len1-i-1+len2-j):len3]
if(i < len1 && mark[i+1][j] == true && s1[i] == s3[len3-(len1-i+len2-j)]){
mark[i][j] = true;
}
}
}
return mark[0][0];
}
};
说明:版权所有,转载请注明出处。 Coder007的博客