Skip to content

Commit 273f37f

Browse files
committedJun 15, 2022
feat: add solutions to lc problem: No.0097
No.0097.Interleaving String
1 parent 5b81255 commit 273f37f

File tree

2 files changed

+213
-1
lines changed

2 files changed

+213
-1
lines changed
 

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

+107-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

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

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

6565
<!-- tabs:start -->
6666

@@ -86,6 +86,42 @@ class Solution:
8686
return dfs(0, 0)
8787
```
8888

89+
```python
90+
class Solution:
91+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
92+
m, n = len(s1), len(s2)
93+
if m + n != len(s3):
94+
return False
95+
dp = [[False] * (n + 1) for _ in range(m + 1)]
96+
dp[0][0] = True
97+
for i in range(m + 1):
98+
for j in range(n + 1):
99+
k = i + j - 1
100+
if i:
101+
dp[i][j] = s1[i - 1] == s3[k] and dp[i - 1][j]
102+
if j:
103+
dp[i][j] |= (s2[j - 1] == s3[k] and dp[i][j - 1])
104+
return dp[-1][-1]
105+
```
106+
107+
```python
108+
class Solution:
109+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
110+
m, n = len(s1), len(s2)
111+
if m + n != len(s3):
112+
return False
113+
dp = [False] * (n + 1)
114+
dp[0] = True
115+
for i in range(m + 1):
116+
for j in range(n + 1):
117+
k = i + j - 1
118+
if i:
119+
dp[j] &= (s1[i - 1] == s3[k])
120+
if j:
121+
dp[j] |= (s2[j - 1] == s3[k] and dp[j - 1])
122+
return dp[-1]
123+
```
124+
89125
### **Java**
90126

91127
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -129,6 +165,31 @@ class Solution {
129165
}
130166
```
131167

168+
```java
169+
class Solution {
170+
public boolean isInterleave(String s1, String s2, String s3) {
171+
int m = s1.length(), n = s2.length();
172+
if (m + n != s3.length()) {
173+
return false;
174+
}
175+
boolean[] dp = new boolean[n + 1];
176+
dp[0] = true;
177+
for (int i = 0; i <= m; ++i) {
178+
for (int j = 0; j <= n; ++j) {
179+
int k = i + j - 1;
180+
if (i > 0) {
181+
dp[j] &= (s1.charAt(i - 1) == s3.charAt(k));
182+
}
183+
if (j > 0) {
184+
dp[j] |= (s2.charAt(j - 1) == s3.charAt(k) && dp[j - 1]);
185+
}
186+
}
187+
}
188+
return dp[n];
189+
}
190+
}
191+
```
192+
132193
### **C++**
133194

134195
```cpp
@@ -158,6 +219,28 @@ public:
158219
};
159220
```
160221

222+
```cpp
223+
class Solution {
224+
public:
225+
bool isInterleave(string s1, string s2, string s3) {
226+
int m = s1.size(), n = s2.size();
227+
if (m + n != s3.size()) return false;
228+
vector<int> dp(n + 1);
229+
dp[0] = 1;
230+
for (int i = 0; i <= m; ++i)
231+
{
232+
for (int j = 0; j <= n; ++j)
233+
{
234+
int k = i + j - 1;
235+
if (i) dp[j] &= (s1[i - 1] == s3[k]);
236+
if (j) dp[j] |= (s2[j - 1] == s3[k] && dp[j - 1]);
237+
}
238+
}
239+
return dp[n];
240+
}
241+
};
242+
```
243+
161244
### **Go**
162245
163246
```go
@@ -189,6 +272,29 @@ func isInterleave(s1 string, s2 string, s3 string) bool {
189272
}
190273
```
191274

275+
```go
276+
func isInterleave(s1 string, s2 string, s3 string) bool {
277+
m, n := len(s1), len(s2)
278+
if m+n != len(s3) {
279+
return false
280+
}
281+
dp := make([]bool, n+1)
282+
dp[0] = true
283+
for i := 0; i <= m; i++ {
284+
for j := 0; j <= n; j++ {
285+
k := i + j - 1
286+
if i > 0 {
287+
dp[j] = dp[j] && (s1[i-1] == s3[k])
288+
}
289+
if j > 0 {
290+
dp[j] = dp[j] || (s2[j-1] == s3[k] && dp[j-1])
291+
}
292+
}
293+
}
294+
return dp[n]
295+
}
296+
```
297+
192298
### **...**
193299

194300
```

‎solution/0000-0099/0097.Interleaving String/README_EN.md

+106
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,42 @@ class Solution:
7575
return dfs(0, 0)
7676
```
7777

78+
```python
79+
class Solution:
80+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
81+
m, n = len(s1), len(s2)
82+
if m + n != len(s3):
83+
return False
84+
dp = [[False] * (n + 1) for _ in range(m + 1)]
85+
dp[0][0] = True
86+
for i in range(m + 1):
87+
for j in range(n + 1):
88+
k = i + j - 1
89+
if i:
90+
dp[i][j] = s1[i - 1] == s3[k] and dp[i - 1][j]
91+
if j:
92+
dp[i][j] |= (s2[j - 1] == s3[k] and dp[i][j - 1])
93+
return dp[-1][-1]
94+
```
95+
96+
```python
97+
class Solution:
98+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
99+
m, n = len(s1), len(s2)
100+
if m + n != len(s3):
101+
return False
102+
dp = [False] * (n + 1)
103+
dp[0] = True
104+
for i in range(m + 1):
105+
for j in range(n + 1):
106+
k = i + j - 1
107+
if i:
108+
dp[j] &= (s1[i - 1] == s3[k])
109+
if j:
110+
dp[j] |= (s2[j - 1] == s3[k] and dp[j - 1])
111+
return dp[-1]
112+
```
113+
78114
### **Java**
79115

80116
```java
@@ -116,6 +152,31 @@ class Solution {
116152
}
117153
```
118154

155+
```java
156+
class Solution {
157+
public boolean isInterleave(String s1, String s2, String s3) {
158+
int m = s1.length(), n = s2.length();
159+
if (m + n != s3.length()) {
160+
return false;
161+
}
162+
boolean[] dp = new boolean[n + 1];
163+
dp[0] = true;
164+
for (int i = 0; i <= m; ++i) {
165+
for (int j = 0; j <= n; ++j) {
166+
int k = i + j - 1;
167+
if (i > 0) {
168+
dp[j] &= (s1.charAt(i - 1) == s3.charAt(k));
169+
}
170+
if (j > 0) {
171+
dp[j] |= (s2.charAt(j - 1) == s3.charAt(k) && dp[j - 1]);
172+
}
173+
}
174+
}
175+
return dp[n];
176+
}
177+
}
178+
```
179+
119180
### **C++**
120181

121182
```cpp
@@ -145,6 +206,28 @@ public:
145206
};
146207
```
147208

209+
```cpp
210+
class Solution {
211+
public:
212+
bool isInterleave(string s1, string s2, string s3) {
213+
int m = s1.size(), n = s2.size();
214+
if (m + n != s3.size()) return false;
215+
vector<int> dp(n + 1);
216+
dp[0] = 1;
217+
for (int i = 0; i <= m; ++i)
218+
{
219+
for (int j = 0; j <= n; ++j)
220+
{
221+
int k = i + j - 1;
222+
if (i) dp[j] &= (s1[i - 1] == s3[k]);
223+
if (j) dp[j] |= (s2[j - 1] == s3[k] && dp[j - 1]);
224+
}
225+
}
226+
return dp[n];
227+
}
228+
};
229+
```
230+
148231
### **Go**
149232
150233
```go
@@ -176,6 +259,29 @@ func isInterleave(s1 string, s2 string, s3 string) bool {
176259
}
177260
```
178261

262+
```go
263+
func isInterleave(s1 string, s2 string, s3 string) bool {
264+
m, n := len(s1), len(s2)
265+
if m+n != len(s3) {
266+
return false
267+
}
268+
dp := make([]bool, n+1)
269+
dp[0] = true
270+
for i := 0; i <= m; i++ {
271+
for j := 0; j <= n; j++ {
272+
k := i + j - 1
273+
if i > 0 {
274+
dp[j] = dp[j] && (s1[i-1] == s3[k])
275+
}
276+
if j > 0 {
277+
dp[j] = dp[j] || (s2[j-1] == s3[k] && dp[j-1])
278+
}
279+
}
280+
}
281+
return dp[n]
282+
}
283+
```
284+
179285
### **...**
180286

181287
```

0 commit comments

Comments
 (0)
Please sign in to comment.