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.
class Solution { public: bool interleave(string s1, int len1 , string s2 , int len2 , string s3,int len3) { if(len1 + len2 != len3) return false; if(len3 == s3.length() ) return true; if(len1 == s1.length()) { if(s2[len2] == s3[len3]) return interleave(s1 , len1 , s2 , len2 + 1 , s3 ,len3 + 1); return false; } if(len2 == s2.length()) { if(s1[len1] == s3[len3]) return interleave(s1 , len1 + 1 , s2 , len2 , s3 , len3 + 1); return false; } if(s1[len1] == s3[len3] && s2[len2] != s3[len3]) return interleave(s1,len1 + 1 , s2,len2, s3,len3 + 1); else if(s1[len1] != s3[len3] && s2[len2] == s3[len3]) return interleave(s1 , len1 , s2 , len2 + 1 , s3 , len3 + 1); else if(s1[len1] == s3[len3] && s2[len2] == s3[len3]) return (interleave(s1 , len1 + 1 , s2 ,len2 , s3 , len3 + 1) || interleave(s1 , len1 , s2 , len2 + 1 , s3 , len3 + 1)); else return false; } bool isInterleave(string s1, string s2, string s3) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s1.length() + s2.length() != s3.length()) return false; return interleave(s1 , 0 , s2 , 0 , s3 , 0); } };