Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2998 from benmak11/minimum-freq
Browse files Browse the repository at this point in the history
Create 1647-minimum-deletions-to-make-character-frequencies-unique.java
  • Loading branch information
MHamiid authored Sep 12, 2023
2 parents bdef290 + dc1bc13 commit 88d14bd
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int minDeletions(String s) {
int[] chars = new int[26];
for (char c : s.toCharArray()) {
chars[c - 'a']++;
}

int[] arr = Arrays.stream(chars).boxed()
.sorted(Collections.reverseOrder())
.mapToInt(Integer::intValue)
.toArray();
int frequency = arr[0];
int res = 0;
for (int i : arr) {
if (i > frequency) {
if (frequency > 0)
res += (i - frequency);
else
res += i;
}
frequency = Math.min(frequency - 1, i - 1);
}
return res;
}
}

0 comments on commit 88d14bd

Please sign in to comment.