Skip to content

Commit 75d71ab

Browse files
committed
feat: add solutions to lc problem: No.1647. Minimum Deletions to Make Character Frequencies Unique
1 parent bdb5622 commit 75d71ab

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
哈希表 + 升序排序
56+
哈希表 + 排序。
5757

5858
<!-- tabs:start -->
5959

@@ -62,15 +62,41 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def minDeletions(self, s: str) -> int:
67+
counter = collections.Counter(s)
68+
vals = [v for v in counter.values()]
69+
vals.sort(reverse=True)
70+
ans = 0
71+
for i in range(1, len(vals)):
72+
while vals[i] >= vals[i - 1] and vals[i] > 0:
73+
vals[i] -= 1
74+
ans += 1
75+
return ans
6676
```
6777

6878
### **Java**
6979

7080
<!-- 这里可写当前语言的特殊实现逻辑 -->
7181

7282
```java
73-
83+
class Solution {
84+
public int minDeletions(String s) {
85+
int[] counter = new int[26];
86+
for (char c : s.toCharArray()) {
87+
++counter[c - 'a'];
88+
}
89+
Arrays.sort(counter);
90+
int ans = 0;
91+
for (int i = 24; i >= 0; --i) {
92+
while (counter[i] >= counter[i + 1] && counter[i] > 0) {
93+
--counter[i];
94+
++ans;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
74100
```
75101

76102
### **TypeScript**

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,39 @@ Note that we only care about characters that are still in the string at the end
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def minDeletions(self, s: str) -> int:
57+
counter = collections.Counter(s)
58+
vals = [v for v in counter.values()]
59+
vals.sort(reverse=True)
60+
ans = 0
61+
for i in range(1, len(vals)):
62+
while vals[i] >= vals[i - 1] and vals[i] > 0:
63+
vals[i] -= 1
64+
ans += 1
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
```java
61-
71+
class Solution {
72+
public int minDeletions(String s) {
73+
int[] counter = new int[26];
74+
for (char c : s.toCharArray()) {
75+
++counter[c - 'a'];
76+
}
77+
Arrays.sort(counter);
78+
int ans = 0;
79+
for (int i = 24; i >= 0; --i) {
80+
while (counter[i] >= counter[i + 1] && counter[i] > 0) {
81+
--counter[i];
82+
++ans;
83+
}
84+
}
85+
return ans;
86+
}
87+
}
6288
```
6389

6490
### **TypeScript**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int minDeletions(String s) {
3+
int[] counter = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++counter[c - 'a'];
6+
}
7+
Arrays.sort(counter);
8+
int ans = 0;
9+
for (int i = 24; i >= 0; --i) {
10+
while (counter[i] >= counter[i + 1] && counter[i] > 0) {
11+
--counter[i];
12+
++ans;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minDeletions(self, s: str) -> int:
3+
counter = collections.Counter(s)
4+
vals = [v for v in counter.values()]
5+
vals.sort(reverse=True)
6+
ans = 0
7+
for i in range(1, len(vals)):
8+
while vals[i] >= vals[i - 1] and vals[i] > 0:
9+
vals[i] -= 1
10+
ans += 1
11+
return ans

0 commit comments

Comments
 (0)