Skip to content

feat: add solutions to lc problem: No.3085 #4512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,63 @@ func minimumDeletions(word string, k int) int {
}
```

#### TypeScript

```ts
function minimumDeletions(word: string, k: number): number {
const freq: number[] = Array(26).fill(0);
for (const ch of word) {
++freq[ch.charCodeAt(0) - 97];
}
const nums = freq.filter(x => x > 0);
const f = (v: number): number => {
let ans = 0;
for (const x of nums) {
if (x < v) {
ans += x;
} else if (x > v + k) {
ans += x - v - k;
}
}
return ans;
};
return Math.min(...Array.from({ length: word.length + 1 }, (_, i) => f(i)));
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_deletions(word: String, k: i32) -> i32 {
let mut freq = [0; 26];
for c in word.chars() {
freq[(c as u8 - b'a') as usize] += 1;
}
let mut nums = vec![];
for &v in freq.iter() {
if v > 0 {
nums.push(v);
}
}
let n = word.len() as i32;
let mut ans = n;
for i in 0..=n {
let mut cur = 0;
for &x in nums.iter() {
if x < i {
cur += x;
} else if x > i + k {
cur += x - i - k;
}
}
ans = ans.min(cur);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,39 @@ function minimumDeletions(word: string, k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_deletions(word: String, k: i32) -> i32 {
let mut freq = [0; 26];
for c in word.chars() {
freq[(c as u8 - b'a') as usize] += 1;
}
let mut nums = vec![];
for &v in freq.iter() {
if v > 0 {
nums.push(v);
}
}
let n = word.len() as i32;
let mut ans = n;
for i in 0..=n {
let mut cur = 0;
for &x in nums.iter() {
if x < i {
cur += x;
} else if x > i + k {
cur += x - i - k;
}
}
ans = ans.min(cur);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
impl Solution {
pub fn minimum_deletions(word: String, k: i32) -> i32 {
let mut freq = [0; 26];
for c in word.chars() {
freq[(c as u8 - b'a') as usize] += 1;
}
let mut nums = vec![];
for &v in freq.iter() {
if v > 0 {
nums.push(v);
}
}
let n = word.len() as i32;
let mut ans = n;
for i in 0..=n {
let mut cur = 0;
for &x in nums.iter() {
if x < i {
cur += x;
} else if x > i + k {
cur += x - i - k;
}
}
ans = ans.min(cur);
}
ans
}
}