Skip to content

Create 3085. Minimum Deletions to Make String K-Special #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 3085. Minimum Deletions to Make String K-Special
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
int minimumDeletions(string word, int k) {
unordered_map <char,int> freq;
for(auto x:word){
freq[x]++;
}
vector<int> storage;
for(auto pair: freq){
storage.push_back(pair.second);
}
sort(storage.begin(),storage.end());
int n=storage.size();
//now we need to check that how many deletion would be needed to make the freq lie in the range of [reference,reference+k] --> so that difference of freq is at most k.
int min_times=1e5;
// for each freq considering it as reference and calcualating deletion
for(int i=0;i<n;i++){
int ref=storage[i];
int deletion=0;
// for freq less than reference, deletion of all the characters
for(int j=0;j<i;j++){
deletion+=storage[j];
}
// for freq greater than ref+k we will reduce them exactly to ref+k
for(int j=i;j<n;j++){
if(storage[j]>ref+k){
deletion+=storage[j]-(ref+k);
}
}
if(deletion<min_times){
min_times=deletion;
}
}
return min_times;
}
};
Loading