1647. Minimum Deletions to Make Character Frequencies Unique
T: O(n + 26log26), n is len of s
S: O(26)```java
class Solution {
    public int minDeletions(String s) {
        int[] count = new int[26];
        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i)-'a']++;
        }
        Arrays.sort(count);
        int keep = count[25];
        int prev = keep;
        for (int i = count.length-2; i >= 0 && count[i] > 0 && prev > 0; i--) {
            prev = Math.min(prev-1, count[i]);
            keep += prev;
        }
        return s.length() - keep;
    }
}
/**
T: O(n + 26log26), n is len of s
S: O(26)
bbcebab
b: 3
c: 1
e: 1
a: 1
ans is 2 
we will delete e and a
so when prev is already 0, means can't keep doing
how to make unique? prev -1
idea:
origin:
3 1 1 1
after
3 1 0 0
origin:
2 2 2
after 
2 1 0
origin:
5 4 4 3
after:
5 4 3 2
so have to do this:
min(prev-1, current value)
 */
```Previous1171. Remove Zero Sum Consecutive Nodes from Linked ListNext1481. Least Number of Unique Integers after K Removals
Last updated
Was this helpful?