/*
Interleaving String
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.
*/
#include<string>
usingnamespace std;
classInterleavingString {
public:
bool isInterleave(std::strings1,strings2,strings3) {
if ((s1.length() + s2.length()) != s3.length())returnfalse;
if (s1.length() == 0)
{
if (s3 == s2)returntrue;
elsereturnfalse;
}
bool* results = newbool[(s1.length() + 1)*(s3.length() + 1)];
results[0] =true;
for (unsignedint indx = 1; indx < (s1.length() + 1)*(s3.length() + 1); indx++)
results[indx] =false;
for (unsignedint len3 = 1; len3 <= s3.length(); len3++)
{
unsignedint startIndx = 0;
unsignedint endIndx = len3;
if (len3 > s2.length()) startIndx = len3 – s2.length();
if (endIndx>s1.length()) endIndx = s1.length();
for (unsignedint len1 = startIndx; len1 <= endIndx; len1++)
{
if (len1>0 && len1 <= s1.length() && s3[len3 – 1] == s1[len1 – 1] && results[(len3 – 1)*(s1.length() + 1) + len1 – 1])
results[len3*(s1.length() + 1) + len1] = true;
if (len1<len3 &&len3 – len1 <= s2.length() && s3[len3 – 1] == s2[len3 – len1 – 1] && results[(len3 – 1)*(s1.length() + 1) + len1])
results[len3*(s1.length() + 1) + len1] = true;
}
}
bool result = results[s3.length()*(s1.length() + 1) + s1.length()];
delete[] results;
return result;
}
};