You are given a string s consisting of lowercase English letters, and an integer k.
Your task is to delete some (possibly none) of the characters in the string so that the number of distinct characters in the resulting string is at mostk.
Return the minimum number of deletions required to achieve this.
Input: s ="abc", k =2Output: 1Explanation:
*`s` has three distinct characters:`'a'`,`'b'` and `'c'`, each with a frequency of 1.* Since we can have at most `k = 2` distinct characters, remove all occurrences of any one character from the string.* For example, removing all occurrences of `'c'` results in at most `k` distinct characters. Thus, the answer is1.
Input: s ="aabb", k =2Output: 0Explanation:
*`s` has two distinct characters(`'a'` and `'b'`)with frequencies of 2 and 2, respectively.* Since we can have at most `k = 2` distinct characters, no deletions are required. Thus, the answer is0.
Input: s ="yyyzz", k =1Output: 2Explanation:
*`s` has two distinct characters(`'y'` and `'z'`)with frequencies of 3 and 2, respectively.* Since we can have at most `k = 1` distinct character, remove all occurrences of any one character from the string.* Removing all `'z'` results in at most `k` distinct characters. Thus, the answer is2.
To minimize deletions, we want to keep the k most frequent characters and remove all others. By sorting character frequencies, we can efficiently determine which characters to delete.
classSolution {
public:int minDeletions(string s, int k) {
unordered_map<char, int> freq;
for (char c : s) freq[c]++;
if (freq.size() <= k) return0;
vector<int> f;
for (auto& p : freq) f.push_back(p.second);
sort(f.begin(), f.end());
int ans =0;
for (int i =0; i < f.size() - k; ++i) ans += f[i];
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
funcminDeletions(sstring, kint) int {
freq:=map[rune]int{}
for_, c:=ranges { freq[c]++ }
if len(freq) <=k { return0 }
f:= []int{}
for_, v:=rangefreq { f = append(f, v) }
sort.Ints(f)
ans:=0fori:=0; i < len(f)-k; i++ { ans+=f[i] }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
publicintminDeletions(String s, int k) {
Map<Character, Integer> freq =new HashMap<>();
for (char c : s.toCharArray()) freq.put(c, freq.getOrDefault(c, 0)+1);
if (freq.size() <= k) return 0;
List<Integer> f =new ArrayList<>(freq.values());
Collections.sort(f);
int ans = 0;
for (int i = 0; i < f.size()-k; ++i) ans += f.get(i);
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funminDeletions(s: String, k: Int): Int {
val freq = mutableMapOf<Char,Int>()
for (c in s) freq[c] = freq.getOrDefault(c,0)+1if (freq.size <= k) return0val f = freq.values.sorted()
return f.take(f.size-k).sum()
}
}
1
2
3
4
5
6
7
8
classSolution:
defminDeletions(self, s: str, k: int) -> int:
from collections import Counter
freq = Counter(s)
if len(freq) <= k:
return0 f = sorted(freq.values())
return sum(f[:len(f)-k])
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnmin_deletions(s: String, k: i32) -> i32 {
use std::collections::HashMap;
letmut freq = HashMap::new();
for c in s.chars() { *freq.entry(c).or_insert(0) +=1; }
if freq.len() asi32<= k { return0; }
letmut f: Vec<i32>= freq.values().cloned().collect();
f.sort();
f.iter().take(f.len()-k asusize).sum()
}
}