Skip to content

Commit 434db7a

Browse files
authored
Create interleaving-string.cpp
1 parent fc5c845 commit 434db7a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

interleaving-string.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
bool isInterleave(string s1, string s2, string s3) {
4+
if (s1.length() + s2.length() != s3.length()) {
5+
return false;
6+
}
7+
8+
std::vector<std::vector<char>> memo(1 + s1.length(), std::vector<char>(1 + s2.length(), -1));
9+
10+
std::function<bool(int,int)> go = [&](int a, int b) -> bool {
11+
12+
int c = a + b;
13+
if (c >= static_cast<int>(s3.length())) {
14+
return true;
15+
}
16+
17+
char& ret = memo[a][b];
18+
if (ret != -1) {
19+
return ret;
20+
}
21+
22+
ret = false;
23+
24+
ret |= (a < static_cast<int>(s1.length()) && s3[c] == s1[a] && go(a+1, b));
25+
ret |= (b < static_cast<int>(s2.length()) && s3[c] == s2[b] && go(a, b+1));
26+
27+
return ret;
28+
};
29+
30+
return go(0,0);
31+
}
32+
};

0 commit comments

Comments
 (0)