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 1187dc8

Browse files
authored
feat: add rust solution to lc problem: No.2707 (#1568)
1 parent 334bdaa commit 1187dc8

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

‎solution/2700-2799/2707.Extra Characters in a String/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,39 @@ public:
112112
};
113113
```
114114
115+
### **Rust**
116+
117+
```rust
118+
use std::collections::HashSet;
119+
120+
impl Solution {
121+
#[allow(dead_code)]
122+
pub fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
123+
let n = s.len();
124+
let mut set = dictionary
125+
.iter()
126+
.map(|s| s.into())
127+
.collect::<HashSet<String>>();
128+
let mut dp = vec![0; n + 1];
129+
130+
// Initialize the dp vector
131+
dp[0] = 0;
132+
133+
// Begin the actual dp process
134+
for i in 1..=n {
135+
dp[i] = dp[i - 1] + 1;
136+
for j in 0..i {
137+
if set.contains(&s[j..i]) {
138+
dp[i] = std::cmp::min(dp[i], dp[j]);
139+
}
140+
}
141+
}
142+
143+
dp[n]
144+
}
145+
}
146+
```
147+
115148
### **Go**
116149

117150
```go

‎solution/2700-2799/2707.Extra Characters in a String/README_EN.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ public:
105105
};
106106
```
107107
108+
### **Rust**
109+
110+
```rust
111+
use std::collections::HashSet;
112+
113+
impl Solution {
114+
#[allow(dead_code)]
115+
pub fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
116+
let n = s.len();
117+
let mut set = dictionary
118+
.iter()
119+
.map(|s| s.into())
120+
.collect::<HashSet<String>>();
121+
let mut dp = vec![0; n + 1];
122+
123+
// Initialize the dp vector
124+
dp[0] = 0;
125+
126+
// Begin the actual dp process
127+
for i in 1..=n {
128+
dp[i] = dp[i - 1] + 1;
129+
for j in 0..i {
130+
if set.contains(&s[j..i]) {
131+
dp[i] = std::cmp::min(dp[i], dp[j]);
132+
}
133+
}
134+
}
135+
136+
dp[n]
137+
}
138+
}
139+
```
140+
108141
### **Go**
109142

110143
```go
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
6+
let n = s.len();
7+
let mut set = dictionary
8+
.iter()
9+
.map(|s| s.into())
10+
.collect::<HashSet<String>>();
11+
let mut dp = vec![0; n + 1];
12+
13+
// Initialize the dp vector
14+
dp[0] = 0;
15+
16+
// Begin the actual dp process
17+
for i in 1..=n {
18+
dp[i] = dp[i - 1] + 1;
19+
for j in 0..i {
20+
if set.contains(&s[j..i]) {
21+
dp[i] = std::cmp::min(dp[i], dp[j]);
22+
}
23+
}
24+
}
25+
26+
dp[n]
27+
}
28+
}

0 commit comments

Comments
(0)

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