File tree 3 files changed +82
-0
lines changed
solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,35 @@ public:
192
192
};
193
193
```
194
194
195
+ ### ** Rust**
196
+
197
+ ``` rust
198
+ impl Solution {
199
+ #[allow(dead_code)]
200
+ pub fn min_deletions (s : String ) -> i32 {
201
+ let mut cnt = vec! [0 ; 26 ];
202
+ let mut ans = 0 ;
203
+
204
+ for c in s . chars () {
205
+ cnt [(c as u8 - 'a' as u8 ) as usize ] += 1 ;
206
+ }
207
+
208
+ cnt . sort_by (| & lhs , & rhs | {
209
+ rhs . cmp (& lhs )
210
+ });
211
+
212
+ for i in 1 .. 26 {
213
+ while cnt [i ] >= cnt [i - 1 ] && cnt [i ] > 0 {
214
+ cnt [i ] -= 1 ;
215
+ ans += 1 ;
216
+ }
217
+ }
218
+
219
+ ans
220
+ }
221
+ }
222
+ ```
223
+
195
224
### ** Go**
196
225
197
226
``` go
Original file line number Diff line number Diff line change @@ -170,6 +170,35 @@ public:
170
170
};
171
171
```
172
172
173
+ ### ** Rust**
174
+
175
+ ``` rust
176
+ impl Solution {
177
+ #[allow(dead_code)]
178
+ pub fn min_deletions (s : String ) -> i32 {
179
+ let mut cnt = vec! [0 ; 26 ];
180
+ let mut ans = 0 ;
181
+
182
+ for c in s . chars () {
183
+ cnt [(c as u8 - 'a' as u8 ) as usize ] += 1 ;
184
+ }
185
+
186
+ cnt . sort_by (| & lhs , & rhs | {
187
+ rhs . cmp (& lhs )
188
+ });
189
+
190
+ for i in 1 .. 26 {
191
+ while cnt [i ] >= cnt [i - 1 ] && cnt [i ] > 0 {
192
+ cnt [i ] -= 1 ;
193
+ ans += 1 ;
194
+ }
195
+ }
196
+
197
+ ans
198
+ }
199
+ }
200
+ ```
201
+
173
202
### ** Go**
174
203
175
204
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn min_deletions ( s : String ) -> i32 {
4
+ let mut cnt = vec ! [ 0 ; 26 ] ;
5
+ let mut ans = 0 ;
6
+
7
+ for c in s. chars ( ) {
8
+ cnt[ ( c as u8 - 'a' as u8 ) as usize ] += 1 ;
9
+ }
10
+
11
+ cnt. sort_by ( |& lhs, & rhs| {
12
+ rhs. cmp ( & lhs)
13
+ } ) ;
14
+
15
+ for i in 1 ..26 {
16
+ while cnt[ i] >= cnt[ i - 1 ] && cnt[ i] > 0 {
17
+ cnt[ i] -= 1 ;
18
+ ans += 1 ;
19
+ }
20
+ }
21
+
22
+ ans
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments