97. 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.
solution:
Not thing to say. A very very simple and naive raw Dynamic Programming. Why it is in hard section?
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
bool f[s1.size() + 1][s2.size() + 1][s3.size() + 1];
memset(f, 0, sizeof(f));
f[0][0][0] = true;
for (int i = 0; i <= s1.size(); ++ i)
for (int j = 0; j <= s2.size(); ++ j)
for (int k = 0; k < s3.size(); ++ k) if (f[i][j][k]) {
if (i < s1.size() && s1[i] == s3[k])
f[i + 1][j][k + 1] = true;
if (j < s2.size() && s2[j] == s3[k])
f[i][j + 1][k + 1] = true;
}
return f[s1.size()][s2.size()][s3.size()];
}
};