Minimize string value | Practice | GeeksforGeeks

Given a string of lowercase alphabets and a number k, the task is to find the minimum value of the string after removal of ‘k’ characters. 
The value of a string is defined as the sum of squares of the count of each distinct character.
For example consider the string “geeks”, here frequencies of characters are g -> 1, e -> 2, k -> 1, s -> 1 and value of the string is 12 + 22 + 12 + 12 = 7
 
Example 1:
Input: S = "abccc", K = 1
Output: 6
Explanation: Remove one 'c', then frequency
will be a -> 1, b -> 1, c -> 2.
12 + 12 + 22 = 6

â€‹Example 2:
Input: S = "aaab", K = 2
Output: 2
Explanation: Remove 2 'a's, then frequency
will be a -> 1, b -> 1.
12 + 12 = 2

Your Task:  
You don't need to read input or print anything. Your task is to complete the function minValue() which takes the string s as inputs and returns the answer.

Expected Time Complexity: O(K*log(|S|))
Expected Auxiliary Space: O(constant)

Constraints:
1 ≤ K, |S| ≤ 104
+2
manishkumar250319992 months ago
int minValue(string S, int K){   unordered_map<char,int>mp;   for(int i=0;i<S.length();i++)      mp[S[i]]++;          vector<int>v;    for(auto x:mp)      v.push_back(x.second);            sort(v.begin(),v.end(),greater<int>());    while(K)    {        v[0]=v[0]-1;        K--;        sort(v.begin(),v.end(),greater<int>());    }    int count=0;    for(auto x:v)    {        if(x>0)        count=count+pow(x,2);    }          return count;            }
+1
badgujarsachin836 months ago
	int minValue(string S, int K)
	{
	    if(K>S.size()){
	        return 0;
	    }
	    // Your code goes here
	    int arr[26]={0};
	    int sum=0;
	    for(int i=0;i<S.size();i++){
	        arr[S[i]-'a']++;
	    }
	    
	    int index=0;
	    while(K--){
	        int maxa=-1;
	        for(int i=0;i<26;i++){
	            if(arr[i]>maxa){
	                maxa=arr[i];
	                index=i;
	                
	            }
	        }
	        arr[index]--;
	     
	    }
	    for(int i=0;i<26;i++){
	        arr[i]=arr[i]*arr[i];
	        sum+=arr[i];
	    }
	    return sum;
	}
+1
sanjitpaul7 months ago
Very Easy Java solution using the hashing concept
https://ide.geeksforgeeks.org/GiJzcwlyl8
 
0
VISHAL SHARMA8 months ago
VISHAL SHARMA
https://uploads.disquscdn.c...
0
Arnav Singh1 year ago
Arnav Singh
IN JAVA  0.1s https://uploads.disquscdn.c...
0
Anant Agarwal2 years ago
Anant Agarwal
In C++int minValue(string s, int k){    int cnt[26]; memset(cnt, 0, sizeof(cnt));    int n = s.size();     for(int i = 0; i < n; i++)        cnt[int(s[i]) - 97]++;     set <pair<int, int="">> mset;    for(int i = 0; i < 26; i++)        mset.insert({-cnt[i], i});     for(int i = 0; i < min(n, k); i++)    {        auto it = mset.begin();        int x = (*it).first;        int y = (*it).second;        mset.erase(*it);        mset.insert({x + 1, y});    }     int ans = 0;    for(auto i : mset)        ans += pow(i.first, 2);    return ans;}
0
Vivek Sharma2 years ago
Vivek Sharma
Use count sort on alphabets, start removing the numbers in decreasing order. Update the count of the last character removed if few of its instances still exist in sorted string.
0
Saurabh Singh2 years ago
Saurabh Singh
In Java:
class Solution {    int minValue(String str,int n) {        HashMap<character, integer=""> charMap = new HashMap<>();        /*Frequency*/        for (char c : str.toCharArray()) {            charMap.put(c, charMap.getOrDefault(c, 0) + 1);        }        /*Storing The Freq*/        PriorityQueue<integer> maxQueue = new PriorityQueue<>(Comparator.reverseOrder());        for (char c : charMap.keySet()) {            maxQueue.add(charMap.get(c));        }        /*Minimising the frequency*/        while (n-- > 0) {            int top = maxQueue.poll();            if (top == 0){                break;            }            top--;            maxQueue.add(top);        }        int sum = 0;        while (!maxQueue.isEmpty()) {            Integer data = maxQueue.poll();            sum += data * data;        }        return sum;    }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.