Skip to content

Commit 47ac746

Browse files
committed
add reorderList.cpp
1 parent 27bc6d4 commit 47ac746

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

C++/isInterleave.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time Complexity: O(m * n)
2+
// Space Complexity: O(m + n)
3+
4+
class Solution {
5+
public:
6+
bool isInterleave(string s1, string s2, string s3) {
7+
if(s1.length() + s2.length() != s3.length()) return false;
8+
9+
if(s1.length() < s2.length()) return isInterleave(s2, s1, s3);
10+
11+
vector<bool> f(s2.length() + 1, true);
12+
13+
for(auto j = 1; j <= s2.length(); ++j) {
14+
f[j] = f[j - 1] && s2[j - 1] == s3[j - 1];
15+
}
16+
17+
for(auto i = 1; i <= s1.length(); ++i) {
18+
f[0] = f[0] && s1[i - 1] == s3[i - 1];
19+
for(auto j = 1; j <= s2.length(); ++j) {
20+
f[j] = (f[j] && s1[i - 1] == s3[i + j - 1])
21+
|| (f[j - 1] && s2[j - 1] == s3[i + j - 1]);
22+
}
23+
}
24+
25+
return f[s2.length()];
26+
}
27+
};

0 commit comments

Comments
 (0)