File tree Expand file tree Collapse file tree 3 files changed +118
-0
lines changed
solution/3000-3099/3085.Minimum Deletions to Make String K-Special Expand file tree Collapse file tree 3 files changed +118
-0
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,63 @@ func minimumDeletions(word string, k int) int {
218218}
219219```
220220
221+ #### TypeScript
222+
223+ ``` ts
224+ function minimumDeletions(word : string , k : number ): number {
225+ const freq: number [] = Array (26 ).fill (0 );
226+ for (const ch of word ) {
227+ ++ freq [ch .charCodeAt (0 ) - 97 ];
228+ }
229+ const nums = freq .filter (x => x > 0 );
230+ const f = (v : number ): number => {
231+ let ans = 0 ;
232+ for (const x of nums ) {
233+ if (x < v ) {
234+ ans += x ;
235+ } else if (x > v + k ) {
236+ ans += x - v - k ;
237+ }
238+ }
239+ return ans ;
240+ };
241+ return Math .min (... Array .from ({ length: word .length + 1 }, (_ , i ) => f (i )));
242+ }
243+ ```
244+
245+ #### Rust
246+
247+ ``` rust
248+ impl Solution {
249+ pub fn minimum_deletions (word : String , k : i32 ) -> i32 {
250+ let mut freq = [0 ; 26 ];
251+ for c in word . chars () {
252+ freq [(c as u8 - b 'a' ) as usize ] += 1 ;
253+ }
254+ let mut nums = vec! [];
255+ for & v in freq . iter () {
256+ if v > 0 {
257+ nums . push (v );
258+ }
259+ }
260+ let n = word . len () as i32 ;
261+ let mut ans = n ;
262+ for i in 0 ..= n {
263+ let mut cur = 0 ;
264+ for & x in nums . iter () {
265+ if x < i {
266+ cur += x ;
267+ } else if x > i + k {
268+ cur += x - i - k ;
269+ }
270+ }
271+ ans = ans . min (cur );
272+ }
273+ ans
274+ }
275+ }
276+ ```
277+
221278<!-- tabs: end -->
222279
223280<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -240,6 +240,39 @@ function minimumDeletions(word: string, k: number): number {
240240}
241241```
242242
243+ #### Rust
244+
245+ ``` rust
246+ impl Solution {
247+ pub fn minimum_deletions (word : String , k : i32 ) -> i32 {
248+ let mut freq = [0 ; 26 ];
249+ for c in word . chars () {
250+ freq [(c as u8 - b 'a' ) as usize ] += 1 ;
251+ }
252+ let mut nums = vec! [];
253+ for & v in freq . iter () {
254+ if v > 0 {
255+ nums . push (v );
256+ }
257+ }
258+ let n = word . len () as i32 ;
259+ let mut ans = n ;
260+ for i in 0 ..= n {
261+ let mut cur = 0 ;
262+ for & x in nums . iter () {
263+ if x < i {
264+ cur += x ;
265+ } else if x > i + k {
266+ cur += x - i - k ;
267+ }
268+ }
269+ ans = ans . min (cur );
270+ }
271+ ans
272+ }
273+ }
274+ ```
275+
243276<!-- tabs: end -->
244277
245278<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn minimum_deletions ( word : String , k : i32 ) -> i32 {
3+ let mut freq = [ 0 ; 26 ] ;
4+ for c in word. chars ( ) {
5+ freq[ ( c as u8 - b'a' ) as usize ] += 1 ;
6+ }
7+ let mut nums = vec ! [ ] ;
8+ for & v in freq. iter ( ) {
9+ if v > 0 {
10+ nums. push ( v) ;
11+ }
12+ }
13+ let n = word. len ( ) as i32 ;
14+ let mut ans = n;
15+ for i in 0 ..=n {
16+ let mut cur = 0 ;
17+ for & x in nums. iter ( ) {
18+ if x < i {
19+ cur += x;
20+ } else if x > i + k {
21+ cur += x - i - k;
22+ }
23+ }
24+ ans = ans. min ( cur) ;
25+ }
26+ ans
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments