We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 27bc6d4 commit 47ac746Copy full SHA for 47ac746
C++/isInterleave.cpp
@@ -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
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