Skip to content

Commit a103cbb

Browse files
committed
feat: add solutions to lc/lcof2 problems
lcof2 No.096 & lc No.0097.Interleaving String
1 parent 24fd64a commit a103cbb

File tree

11 files changed

+529
-35
lines changed

11 files changed

+529
-35
lines changed

lcof2/剑指 Offer II 096. 字符串交织/README.md

+111
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,133 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
题目描述带有一定迷惑性,“交织”的过程其实就类似归并排序的 merge 过程,每次从 `s1``s2` 的首部取一个字符,最终组成 `s3`,用记忆化搜索或者动态规划都可以解决
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```python
72+
class Solution:
73+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
74+
m, n = len(s1), len(s2)
75+
if m + n != len(s3):
76+
return False
77+
78+
@lru_cache
79+
def dfs(i, j):
80+
if i == m and j == n:
81+
return True
82+
83+
return i < m and s1[i] == s3[i + j] and dfs(i + 1, j) or \
84+
j < n and s2[j] == s3[i + j] and dfs(i, j + 1)
7085

86+
return dfs(0, 0)
7187
```
7288

7389
### **Java**
7490

7591
<!-- 这里可写当前语言的特殊实现逻辑 -->
7692

7793
```java
94+
class Solution {
95+
private int m;
96+
private int n;
97+
private String s1;
98+
private String s2;
99+
private String s3;
100+
private Map<Integer, Boolean> memo = new HashMap<>();
101+
102+
public boolean isInterleave(String s1, String s2, String s3) {
103+
m = s1.length();
104+
n = s2.length();
105+
this.s1 = s1;
106+
this.s2 = s2;
107+
this.s3 = s3;
108+
if (m + n != s3.length()) {
109+
return false;
110+
}
111+
return dfs(0, 0);
112+
}
113+
114+
private boolean dfs(int i, int j) {
115+
System.out.println(i + ", " + j);
116+
if (i == m && j == n) {
117+
return true;
118+
}
119+
if (memo.containsKey(i * 100 + j)) {
120+
return memo.get(i * 100 + j);
121+
}
122+
123+
boolean ret = (i < m && s1.charAt(i) == s3.charAt(i + j) && dfs(i + 1, j)) ||
124+
(j < n && s2.charAt(j) == s3.charAt(i + j) && dfs(i, j + 1));
125+
126+
memo.put(i * 100 + j, ret);
127+
return ret;
128+
}
129+
}
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
bool isInterleave(string s1, string s2, string s3) {
138+
int m = s1.size(), n = s2.size();
139+
if (m + n != s3.size()) return false;
140+
141+
unordered_map<int, bool> memo;
142+
143+
function<bool(int, int)> dfs;
144+
dfs = [&](int i, int j) {
145+
if (i == m && j == n) return true;
146+
auto it = memo.find(i * 100 + j);
147+
if (it != memo.end()) return it->second;
148+
149+
bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) ||
150+
(j < n && s2[j] == s3[i + j] && dfs(i, j + 1));
151+
152+
memo[i * 100 + j] = ret;
153+
return ret;
154+
};
155+
156+
return dfs(0, 0);
157+
}
158+
};
159+
```
160+
161+
### **Go**
162+
163+
```go
164+
func isInterleave(s1 string, s2 string, s3 string) bool {
165+
m, n := len(s1), len(s2)
166+
if m+n != len(s3) {
167+
return false
168+
}
169+
170+
memo := make(map[int]bool)
171+
172+
var dfs func(int, int) bool
173+
dfs = func(i, j int) bool {
174+
if i == m && j == n {
175+
return true
176+
}
177+
if v, ok := memo[i*100+j]; ok {
178+
return v
179+
}
180+
181+
ret := (i < m && s1[i] == s3[i+j] && dfs(i+1, j)) ||
182+
(j < n && s2[j] == s3[i+j] && dfs(i, j+1))
183+
184+
memo[i*100+j] = ret
185+
return ret
186+
}
78187

188+
return dfs(0, 0)
189+
}
79190
```
80191

81192
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool isInterleave(string s1, string s2, string s3) {
4+
int m = s1.size(), n = s2.size();
5+
if (m + n != s3.size()) return false;
6+
7+
unordered_map<int, bool> memo;
8+
9+
function<bool(int, int)> dfs;
10+
dfs = [&](int i, int j) {
11+
if (i == m && j == n) return true;
12+
auto it = memo.find(i * 100 + j);
13+
if (it != memo.end()) return it->second;
14+
15+
bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) ||
16+
(j < n && s2[j] == s3[i + j] && dfs(i, j + 1));
17+
18+
memo[i * 100 + j] = ret;
19+
return ret;
20+
};
21+
22+
return dfs(0, 0);
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func isInterleave(s1 string, s2 string, s3 string) bool {
2+
m, n := len(s1), len(s2)
3+
if m+n != len(s3) {
4+
return false
5+
}
6+
7+
memo := make(map[int]bool)
8+
9+
var dfs func(int, int) bool
10+
dfs = func(i, j int) bool {
11+
if i == m && j == n {
12+
return true
13+
}
14+
if v, ok := memo[i*100+j]; ok {
15+
return v
16+
}
17+
18+
ret := (i < m && s1[i] == s3[i+j] && dfs(i+1, j)) ||
19+
(j < n && s2[j] == s3[i+j] && dfs(i, j+1))
20+
21+
memo[i*100+j] = ret
22+
return ret
23+
}
24+
25+
return dfs(0, 0)
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
private int m;
3+
private int n;
4+
private String s1;
5+
private String s2;
6+
private String s3;
7+
private Map<Integer, Boolean> memo = new HashMap<>();
8+
9+
public boolean isInterleave(String s1, String s2, String s3) {
10+
m = s1.length();
11+
n = s2.length();
12+
this.s1 = s1;
13+
this.s2 = s2;
14+
this.s3 = s3;
15+
if (m + n != s3.length()) {
16+
return false;
17+
}
18+
return dfs(0, 0);
19+
}
20+
21+
private boolean dfs(int i, int j) {
22+
System.out.println(i + ", " + j);
23+
if (i == m && j == n) {
24+
return true;
25+
}
26+
if (memo.containsKey(i * 100 + j)) {
27+
return memo.get(i * 100 + j);
28+
}
29+
30+
boolean ret = (i < m && s1.charAt(i) == s3.charAt(i + j) && dfs(i + 1, j)) ||
31+
(j < n && s2.charAt(j) == s3.charAt(i + j) && dfs(i, j + 1));
32+
33+
memo.put(i * 100 + j, ret);
34+
return ret;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
3+
m, n = len(s1), len(s2)
4+
if m + n != len(s3):
5+
return False
6+
7+
@lru_cache
8+
def dfs(i, j):
9+
if i == m and j == n:
10+
return True
11+
12+
return i < m and s1[i] == s3[i + j] and dfs(i + 1, j) or \
13+
j < n and s2[j] == s3[i + j] and dfs(i, j + 1)
14+
15+
return dfs(0, 0)

solution/0000-0099/0097.Interleaving String/README.md

+111
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,133 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
题目描述带有一定迷惑性,“交错”的过程其实就类似归并排序的 merge 过程,每次从 `s1``s2` 的首部取一个字符,最终组成 `s3`,用记忆化搜索或者动态规划都可以解决
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**
6264

6365
<!-- 这里可写当前语言的特殊实现逻辑 -->
6466

6567
```python
68+
class Solution:
69+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
70+
m, n = len(s1), len(s2)
71+
if m + n != len(s3):
72+
return False
73+
74+
@lru_cache
75+
def dfs(i, j):
76+
if i == m and j == n:
77+
return True
78+
79+
return i < m and s1[i] == s3[i + j] and dfs(i + 1, j) or \
80+
j < n and s2[j] == s3[i + j] and dfs(i, j + 1)
6681

82+
return dfs(0, 0)
6783
```
6884

6985
### **Java**
7086

7187
<!-- 这里可写当前语言的特殊实现逻辑 -->
7288

7389
```java
90+
class Solution {
91+
private int m;
92+
private int n;
93+
private String s1;
94+
private String s2;
95+
private String s3;
96+
private Map<Integer, Boolean> memo = new HashMap<>();
97+
98+
public boolean isInterleave(String s1, String s2, String s3) {
99+
m = s1.length();
100+
n = s2.length();
101+
this.s1 = s1;
102+
this.s2 = s2;
103+
this.s3 = s3;
104+
if (m + n != s3.length()) {
105+
return false;
106+
}
107+
return dfs(0, 0);
108+
}
109+
110+
private boolean dfs(int i, int j) {
111+
System.out.println(i + ", " + j);
112+
if (i == m && j == n) {
113+
return true;
114+
}
115+
if (memo.containsKey(i * 100 + j)) {
116+
return memo.get(i * 100 + j);
117+
}
118+
119+
boolean ret = (i < m && s1.charAt(i) == s3.charAt(i + j) && dfs(i + 1, j)) ||
120+
(j < n && s2.charAt(j) == s3.charAt(i + j) && dfs(i, j + 1));
121+
122+
memo.put(i * 100 + j, ret);
123+
return ret;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
bool isInterleave(string s1, string s2, string s3) {
134+
int m = s1.size(), n = s2.size();
135+
if (m + n != s3.size()) return false;
136+
137+
unordered_map<int, bool> memo;
138+
139+
function<bool(int, int)> dfs;
140+
dfs = [&](int i, int j) {
141+
if (i == m && j == n) return true;
142+
auto it = memo.find(i * 100 + j);
143+
if (it != memo.end()) return it->second;
144+
145+
bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) ||
146+
(j < n && s2[j] == s3[i + j] && dfs(i, j + 1));
147+
148+
memo[i * 100 + j] = ret;
149+
return ret;
150+
};
151+
152+
return dfs(0, 0);
153+
}
154+
};
155+
```
156+
157+
### **Go**
158+
159+
```go
160+
func isInterleave(s1 string, s2 string, s3 string) bool {
161+
m, n := len(s1), len(s2)
162+
if m+n != len(s3) {
163+
return false
164+
}
165+
166+
memo := make(map[int]bool)
167+
168+
var dfs func(int, int) bool
169+
dfs = func(i, j int) bool {
170+
if i == m && j == n {
171+
return true
172+
}
173+
if v, ok := memo[i*100+j]; ok {
174+
return v
175+
}
176+
177+
ret := (i < m && s1[i] == s3[i+j] && dfs(i+1, j)) ||
178+
(j < n && s2[j] == s3[i+j] && dfs(i, j+1))
179+
180+
memo[i*100+j] = ret
181+
return ret
182+
}
74183

184+
return dfs(0, 0)
185+
}
75186
```
76187

77188
### **...**

0 commit comments

Comments
 (0)