File tree 3 files changed +94
-0
lines changed
3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,39 @@ public:
112
112
};
113
113
```
114
114
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
+
115
148
### ** Go**
116
149
117
150
``` go
Original file line number Diff line number Diff line change @@ -105,6 +105,39 @@ public:
105
105
};
106
106
```
107
107
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
+
108
141
### ** Go**
109
142
110
143
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments