Minimum Deletions to Make Character Frequencies Unique LeetCode solution in Java

1
class Solution {
2
private int N = 26;
3

4
public int minDeletions(String s) {
5
int[] array = new int[N];
6
for (char ch : s.toCharArray()) {
7
array[ch - 'a']++;
8
}
9
int ans = 0;
10
Set<Integer> set = new HashSet<>();
11
for (int i : array) {
12
if (i == 0) continue;
13
while (set.contains(i)) {
14
i--;
15
ans++;
16
}
17
if (i != 0) {
18
set.add(i);
19
}
20
}
21
return ans;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0