-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
31 lines (29 loc) · 877 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique
class Solution {
public int minDeletions(String s) {
int freq[] = new int[26];
for(int i=0;i<s.length();i++){
freq[s.charAt(i) - 'a'] ++;
}
HashSet<Integer> set = new HashSet<>();
int ans = 0;
for(int val : freq){
if(val == 0){
continue;
}else if(set.contains(val)){
for(int j=1;j<=val;j++){
if(!set.contains(val - j)){
ans += j;
if(val != j){
set.add(val - j);
}
break;
}
}
}else{
set.add(val);
}
}
return ans;
}
}