Input: word ="aabcaba", k =0Output: 3Explanation: We can make word 0-special by deleting 2 occurrences of "a" and 1 occurrence of "c". Therefore, word becomes equal to "baba" where freq('a')== freq('b')==2.
Input: word ="dabdcbdcdcd", k =2Output: 2Explanation: We can make word 2-special by deleting 1 occurrence of "a" and 1 occurrence of "d". Therefore, word becomes equal to "bdcbdcdcd" where freq('b')==2, freq('c')==3, and freq('d')==4.
Input: word ="aaabaaa", k =2Output: 1Explanation: We can make word 2-special by deleting 1 occurrence of "b". Therefore, word becomes equal to "aaaaaa" where each letter's frequency is now uniformly 6.
To make the string k-special, the difference between the maximum and minimum frequency of any character must not exceed k. By deleting characters, we can reduce the frequencies to fit this constraint. The optimal way is to try all possible target frequencies and calculate the minimum deletions needed for each, then pick the smallest.
classSolution {
public:int minimumDeletions(string word, int k) {
vector<int> freq(26, 0);
for (char c : word) freq[c -'a']++;
vector<int> f;
for (int x : freq) if (x) f.push_back(x);
sort(f.begin(), f.end());
int ans = word.size();
for (int i =0; i < f.size(); ++i) {
int base = f[i], del =0;
for (int j =0; j < f.size(); ++j) {
if (f[j] < base) del += f[j];
elseif (f[j] > base + k) del += f[j] - (base + k);
}
ans = min(ans, del);
}
return ans;
}
};
funcminimumDeletions(wordstring, kint) int {
freq:= make([]int, 26)
for_, c:=rangeword {
freq[c-'a']++ }
f:= []int{}
for_, x:=rangefreq {
ifx > 0 {
f = append(f, x)
}
}
sort.Ints(f)
ans:= len(word)
fori:=0; i < len(f); i++ {
base, del:=f[i], 0for_, v:=rangef {
ifv < base {
del+=v } elseifv > base+k {
del+=v- (base+k)
}
}
ifdel < ans {
ans = del }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
publicintminimumDeletions(String word, int k) {
int[] freq =newint[26];
for (char c : word.toCharArray()) freq[c -'a']++;
List<Integer> f =new ArrayList<>();
for (int x : freq) if (x > 0) f.add(x);
Collections.sort(f);
int ans = word.length();
for (int i = 0; i < f.size(); ++i) {
int base = f.get(i), del = 0;
for (int v : f) {
if (v < base) del += v;
elseif (v > base + k) del += v - (base + k);
}
ans = Math.min(ans, del);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funminimumDeletions(word: String, k: Int): Int {
val freq = IntArray(26)
for (c in word) freq[c - 'a']++val f = freq.filter { it > 0 }.sorted()
var ans = word.length
for (i in f.indices) {
val base = f[i]
var del = 0for (v in f) {
if (v < base) del += v
elseif (v > base + k) del += v - (base + k)
}
ans = minOf(ans, del)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defminimumDeletions(self, word: str, k: int) -> int:
freq: list[int] = [0] *26for c in word:
freq[ord(c) - ord('a')] +=1 f: list[int] = [x for x in freq if x >0]
f.sort()
ans: int = len(word)
for i in range(len(f)):
base = f[i]
del_cnt =0for v in f:
if v < base:
del_cnt += v
elif v > base + k:
del_cnt += v - (base + k)
ans = min(ans, del_cnt)
return ans
impl Solution {
pubfnminimum_deletions(word: String, k: i32) -> i32 {
letmut freq = [0; 26];
for c in word.chars() {
freq[(c asu8-b'a') asusize] +=1;
}
letmut f: Vec<i32>= freq.iter().filter(|&&x| x >0).map(|&x| x).collect();
f.sort();
letmut ans = word.len() asi32;
for&base in&f {
letmut del =0;
for&v in&f {
if v < base {
del += v;
} elseif v > base + k {
del += v - (base + k);
}
}
ans = ans.min(del);
}
ans
}
}