Skip to content

Commit abf9860

Browse files
add 97
1 parent 61d7bef commit abf9860

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ LeetCode
7676
|0094|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | c | [c++](./src/0094-Binary-Tree-Inorder-Traversal/0094.cpp) |[python](./src/0094-Binary-Tree-Inorder-Traversal/0094.py)|||Medium|
7777
|0095|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/) | c | [c++](./src/0095-Unique-Binary-Search-Trees-II/0095.cpp) |[python](./src/0095-Unique-Binary-Search-Trees-II/0095.py)|||Medium|
7878
|0096|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) | c | [c++](./src/0096-Unique-Binary-Search-Trees/0096.cpp) |[python](./src/0096-Unique-Binary-Search-Trees/0096.py)|||Medium|
79+
|0097|[Interleaving String](https://leetcode.com/problems/interleaving-string/) | c | [c++](./src/0097-Interleaving-String/0097.cpp) |[python](./src/0097-Interleaving-String/0097.py)|||Medium|
7980
|0098|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | c | [c++](./src/0098-Validate-Binary-Search-Tree/0098.cpp) |[python](./src/0098-Validate-Binary-Search-Tree/0098.py)|||Medium|
8081
|0100|[Same Tree](https://leetcode.com/problems/same-tree/) | c | [c++](./src/0100-Same-Tree/0100.cpp) |[python](./src/0100-Same-Tree/0100.py)|||Easy|
8182
|0102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | c | [c++](./src/0102-Binary-Tree-Level-Order-Traversal/0102.cpp) |[python](./src/0102-Binary-Tree-Level-Order-Traversal/0102.py)|||Medium|

src/0097-Interleaving-String/0097.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <deque>
2+
#include <string>
3+
using namespace std;
4+
5+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
6+
class Solution
7+
{
8+
public:
9+
bool isInterleave(string s1, string s2, string s3)
10+
{
11+
if (s1.size() + s2.size() != s3.size()) return false;
12+
deque<deque<bool>> mem(s1.size() + 1, deque<bool>(s2.size() + 1, false));
13+
for (int i = 0; i <= s1.size(); ++i)
14+
{
15+
for (int j = 0; j <= s2.size(); ++j)
16+
{
17+
if (i == 0 and j == 0) mem[i][j] = true;
18+
else if (i == 0) mem[i][j] = mem[i][j - 1] and (s2[j - 1] == s3[i + j - 1]);
19+
else if (j == 0) mem[i][j] = mem[i - 1][j] and (s1[i - 1] == s3[i + j - 1]);
20+
else mem[i][j] = (mem[i][j - 1] and (s2[j - 1] == s3[i + j - 1])) or (
21+
mem[i - 1][j] and (s1[i - 1] == s3[i + j - 1]));
22+
}
23+
}
24+
return mem[s1.size()][s2.size()];
25+
}
26+
};

src/0097-Interleaving-String/0097.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def isInterleave(self, s1, s2, s3):
3+
"""
4+
:type s1: str
5+
:type s2: str
6+
:type s3: str
7+
:rtype: bool
8+
"""
9+
def helper(i, j, k, mem):
10+
if (i, j) in mem:
11+
return mem[(i, j)]
12+
13+
if k == len(s3):
14+
return True
15+
16+
if i < len(s1) and s1[i] == s3[k]:
17+
if (helper(i+1, j, k + 1, mem)):
18+
mem[(i, j)] = True
19+
return True
20+
21+
if j < len(s2) and s2[j] == s3[k]:
22+
if helper(i, j+1, k+1, mem):
23+
mem[(i, j)] = True
24+
return True
25+
26+
mem[(i, j)] = False
27+
return False
28+
29+
if len(s1)+len(s2) != len(s3):
30+
return False
31+
32+
return helper(0, 0, 0, {})

0 commit comments

Comments
 (0)