-
Notifications
You must be signed in to change notification settings - Fork 65
/
sol.java
35 lines (27 loc) · 1.02 KB
/
sol.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
32
33
34
35
import java.util.*;
public class sol {
public static void main(String[] args) {
// call the fn here
}
public class Solution {
public int minDeletions(String s) {
int[] freq = new int[26]; // Create an array to store character frequencies
for (char c : s.toCharArray()) {
freq[c - 'a']++; // Count the frequency of each character
}
Arrays.sort(freq); // Sort frequencies in ascending order
int del = 0; // Initialize the deletion count
for (int i = 24; i >= 0; i--) {
if (freq[i] == 0) {
break; // No more characters with this frequency
}
if (freq[i] >= freq[i + 1]) {
int prev = freq[i];
freq[i] = Math.max(0, freq[i + 1] - 1);
del += prev - freq[i]; // Update the deletion count
}
}
return del; // Return the minimum deletions required
}
}
}