forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2998 from benmak11/minimum-freq
Create 1647-minimum-deletions-to-make-character-frequencies-unique.java
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
java/1647-minimum-deletions-to-make-character-frequencies-unique.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |