97.
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
if (len3 != len1 + len2)
return false;
if (len1 == 0 and s2 == s3 or len2 == 0 and s1 == s3)
return true;
if (len1 == 0 and len2 == 0 and len3 == 0)
return true;
vector<vector<int> >dp(len1 + 1, vector<int>(len2 + 1, 0));
for (int i = 0; i <= len1; ++i) {
for (int j = 0; j <= len2; ++j) {
if (i == 0 and j == 0) {
dp[0][0] = 1;
continue;
}
if (j > 0 and s3[i + j - 1] == s2[j - 1])
dp[i][j] |= dp[i][j - 1];
if (i > 0 and s3[i + j - 1] == s1[i - 1])
dp[i][j] |= dp[i - 1][j];
}
}
return dp[len1][len2];
}
};
标签:return,s3,s2,力扣,len2,len1,dp
From: https://www.cnblogs.com/shinidetiehanhan/p/16705639.html