Problem statement

https://binarysearch.com/problems/K-Unique-String/

Solution

Calculate counter, then choose the smallest m - k frequencies and flip them.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, s, k):
        c = sorted(list(Counter(s).values()))
        return sum(c) - sum(c[-k:])