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
O(n^2)
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if (s3.length() != s1.length() + s2.length())
return false;
int len1 = s1.length(), len2 = s2.length();
vector<vector<bool> > dp(len1 + 1, vector<bool>(len2 + 1, true));
// init
for (int i = 1; i <= len1; ++i)
dp[i][0] = dp[i - 1][0] && (s1[i - 1] == s3[i - 1]);
for (int j = 1; j <= len2; ++j)
dp[0][j] = dp[0][j - 1] && (s2[j - 1] == s3[j - 1]);
// dp
for (int i = 1; i <= len1; ++i)
for (int j = 1; j <= len2; ++j)
dp[i][j] = (dp[i - 1][j] && s3[i + j - 1] == s1[i - 1]) ||
(dp[i][j - 1] && s3[i + j - 1] == s2[j - 1]);
return dp[len1][len2];
}
};