Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.code
Given:
s1 = "aabcc",
s2 = "dbbca",orm
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.ip
class Solution { public: bool isInterleave(string s1, string s2, string s3) { int len1 = s1.size(); int len2 = s2.size(); int len3 = s3.size(); if(len1 + len2 != len3) return false; return judge(s1, len1 - 1, s2, len2 - 1, s3, len3 - 1); } private: bool judge(string &s1, int i, string& s2, int j, string &s3, int k) { if(i < 0 && j < 0 && k < 0) return true; if(k < 0) return false; if(i >= 0 && j >= 0 && s1[i] == s2[j]) { if(s3[k] != s1[i]) return false; if(judge(s1, i - 1, s2, j, s3, k - 1) || judge(s1, i, s2, j - 1, s3, k - 1)) return true; } else if(i >= 0 && s1[i] == s3[k]) return judge(s1, i - 1, s2, j, s3, k - 1); else if(j >= 0 && s2[j] == s3[k]) return judge(s1, i, s2, j - 1, s3, k - 1); return false; } };
class Solution { public: bool isInterleave(string s1, string s2, string s3) { int len1 = s1.size(); int len2 = s2.size(); int len3 = s3.size(); if(len1 + len2 != len3) return false; vector<vector<bool>> dp(len1 + 1, vector<bool>(len2 + 1, false)); for(int i = 0; i <= len1; ++i) { for(int j = 0; j <= len2; ++j) { if(i == 0 && j == 0) dp[i][j] = true; else if(i == 0) { dp[i][j] = dp[i][j - 1] && (s2[j - 1] == s3[i + j - 1]); } else if(j == 0) { dp[i][j] = dp[i - 1][j] && (s1[i - 1] == s3[i + j - 1]); } else { 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])); } } } return dp[len1][len2]; } };