Skip to content

Commit 6f06972

Browse files
authored
feat: add rust solution to lc problem: No.1647 (#1610)
1 parent 187ac6c commit 6f06972

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,35 @@ public:
192192
};
193193
```
194194

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+
195224
### **Go**
196225

197226
```go

solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,35 @@ public:
170170
};
171171
```
172172

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+
173202
### **Go**
174203

175204
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)