Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2.
Have you met this question in a real interview?
Yes
Example
For s1 =
"aabcc"
, s2 = "dbbca"
- When s3 =
"aadbbcbcac"
, returntrue
. - When s3 =
"aadbbbaccc"
, returnfalse
.
Challenge
class Solution {
public:
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true of false.
*/
bool isInterleave(string s1, string s2, string s3) {
// write your code here
int m=s1.size();
int n=s2.size();
if (m+n!=s3.size())
return false;
// i from s1 and j from s2 can form interleave s3 with i+j
vector< vector
O(n2) time or better
No comments:
Post a Comment