Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b18c822

Browse files
authored
feat: add rust solution to lc problem: No.0097 (doocs#1510)
1 parent a4dad0b commit b18c822

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,53 @@ public:
347347
};
348348
```
349349
350+
### **Rust**
351+
352+
```rust
353+
impl Solution {
354+
#[allow(dead_code)]
355+
pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
356+
let n = s1.len();
357+
let m = s2.len();
358+
359+
if s1.len() + s2.len() != s3.len() {
360+
return false;
361+
}
362+
363+
let mut record = vec![vec![-1; m + 1]; n + 1];
364+
365+
Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect())
366+
}
367+
368+
#[allow(dead_code)]
369+
fn dfs(record: &mut Vec<Vec<i32>>, n: usize, m: usize, i: usize, j: usize, s1: &Vec<char>, s2: &Vec<char>, s3: &Vec<char>) -> bool {
370+
if i >= n && j >= m {
371+
return true;
372+
}
373+
374+
if record[i][j] != -1 {
375+
return record[i][j] == 1;
376+
}
377+
378+
// Set the initial value
379+
record[i][j] = 0;
380+
let k = i + j;
381+
382+
// Let's try `s1` first
383+
if i < n && s1[i] == s3[k] && Self::dfs(record, n, m, i + 1, j, s1, s2, s3) {
384+
record[i][j] = 1;
385+
}
386+
387+
// If the first approach does not succeed, let's then try `s2`
388+
if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) {
389+
record[i][j] = 1;
390+
}
391+
392+
record[i][j] == 1
393+
}
394+
}
395+
```
396+
350397
### **Go**
351398

352399
```go

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,53 @@ public:
299299
};
300300
```
301301
302+
### **Rust**
303+
304+
```rust
305+
impl Solution {
306+
#[allow(dead_code)]
307+
pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
308+
let n = s1.len();
309+
let m = s2.len();
310+
311+
if s1.len() + s2.len() != s3.len() {
312+
return false;
313+
}
314+
315+
let mut record = vec![vec![-1; m + 1]; n + 1];
316+
317+
Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect())
318+
}
319+
320+
#[allow(dead_code)]
321+
fn dfs(record: &mut Vec<Vec<i32>>, n: usize, m: usize, i: usize, j: usize, s1: &Vec<char>, s2: &Vec<char>, s3: &Vec<char>) -> bool {
322+
if i >= n && j >= m {
323+
return true;
324+
}
325+
326+
if record[i][j] != -1 {
327+
return record[i][j] == 1;
328+
}
329+
330+
// Set the initial value
331+
record[i][j] = 0;
332+
let k = i + j;
333+
334+
// Let's try `s1` first
335+
if i < n && s1[i] == s3[k] && Self::dfs(record, n, m, i + 1, j, s1, s2, s3) {
336+
record[i][j] = 1;
337+
}
338+
339+
// If the first approach does not succeed, let's then try `s2`
340+
if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) {
341+
record[i][j] = 1;
342+
}
343+
344+
record[i][j] == 1
345+
}
346+
}
347+
```
348+
302349
### **Go**
303350

304351
```go
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
4+
let n = s1.len();
5+
let m = s2.len();
6+
7+
if s1.len() + s2.len() != s3.len() {
8+
return false;
9+
}
10+
11+
let mut record = vec![vec![-1; m + 1]; n + 1];
12+
13+
Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect())
14+
}
15+
16+
#[allow(dead_code)]
17+
fn dfs(record: &mut Vec<Vec<i32>>, n: usize, m: usize, i: usize, j: usize, s1: &Vec<char>, s2: &Vec<char>, s3: &Vec<char>) -> bool {
18+
if i >= n && j >= m {
19+
return true;
20+
}
21+
22+
if record[i][j] != -1 {
23+
return record[i][j] == 1;
24+
}
25+
26+
// Set the initial value
27+
record[i][j] = 0;
28+
let k = i + j;
29+
30+
// Let's try `s1` first
31+
if i < n && s1[i] == s3[k] && Self::dfs(record, n, m, i + 1, j, s1, s2, s3) {
32+
record[i][j] = 1;
33+
}
34+
35+
// If the first approach does not succeed, let's then try `s2`
36+
if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) {
37+
record[i][j] = 1;
38+
}
39+
40+
record[i][j] == 1
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /