main nahi bataunga

 avatar
bruteCoder
java
2 months ago
814 B
1
Indexable
Never

class Solution{
    static int minValue(String s, int k){
        // code here
        
        HashMap<Character,Integer> map = new HashMap<>();
        
        for(char el : s.toCharArray()){
            map.put(el,map.getOrDefault(el,0)+1);
        }
        
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        
        for(int el : map.values()){
          pq.offer(el);
        }
        
        while(!pq.isEmpty() && k-->0){
            int temp = pq.poll();
            
            temp -= 1;
            
            if(temp == 0 ) continue;
            
            pq.offer(temp);
        }
        
        int out = 0 ;
        
        
        for(int temp : pq){
             out += (temp * temp);
        }
        
        return out;
       
    }
}
Leave a Comment