forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interleaving_String.cc
47 lines (47 loc) · 1.38 KB
/
Interleaving_String.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
if (len1 + len2 != len3) {
return false;
}
vector<vector<bool> > dp;
vector<bool> tmp;
for (int i = 0; i <=len2; i++) {
tmp.push_back(false);
}
for (int i = 0; i <= len1; i++) {
dp.push_back(tmp);
}
dp[0][0] = true;
for (int i = 1; i <= len1; i++) {
if (s1[i - 1] == s3[i - 1]) {
dp[i][0] = true;
} else {
break;
}
}
for (int i = 1; i <= len2; i++) {
if (s2[i - 1] == s3[i - 1]) {
dp[0][i] = true;
} else {
break;
}
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (s1[i - 1] == s3[i + j - 1]) {
dp[i][j] = dp[i][j] | dp[i - 1][j];
}
if (s2[j - 1] == s3[i + j - 1]) {
dp[i][j] = dp[i][j] | dp[i][j - 1];
}
}
}
return dp[len1][len2];
}
};