forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0097.cpp
More file actions
23 lines (20 loc) · 782 Bytes
/
0097.cpp
File metadata and controls
23 lines (20 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if (s1.length() + s2.length() != s3.length()) return false;
vector<bool> dp(s2.length() + 1);
for (int i = 0; i <= s1.length(); i++)
for (int j = 0; j <= s2.length(); j++) {
if (i == 0 && j == 0)
dp[j] = true;
else if (i == 0)
dp[j] = dp[j - 1] && s2[j - 1] == s3[i + j - 1];
else if (j == 0)
dp[j] = dp[j] && s1[i - 1] == s3[i + j - 1];
else
dp[j] = (dp[j] && s1[i - 1] == s3[i + j - 1]) ||
(dp[j - 1] && s2[j - 1] == s3[i + j - 1]);
}
return dp[s2.length()];
}
};