![]() |
VOOZH | about |
Given a string S of length n, consisting of lowercase English letters. The task is to check if it is possible to remove at most K letter from S such that the frequency of every remaining letter is equal.
Examples:
Input: S = "aabbcc", K = 2
Output: True
Explanation: We can remove one 'a' and one 'b' to get "abbcc", where the frequency of each remaining letter is 2Input: S = "eaabbccd", K = 1
Output: False
Explanation: Removing any one letter won't be enough to make the frequency of every remaining letter equal.
Approach:
The goal is to see if we can remove up to K letters to make all remaining letters in S have the same frequency. To achieve this, we can follow these steps:
- Count the frequency of each letter in the string.
- Determine the unique frequencies of these counts.
- Analyze if adjusting these frequencies by removing up to K letters can make the frequencies equal.
Step-by-Step Approach:
True.False.Below is the implementation of the above approach:
Example 1: True Example 2: False
Time Complexity: O(n + m*logm) Counting frequencies takes O(n). Sorting the frequencies takes O(m*logm) where m is the number of unique characters (at most 26 for lowercase English letters).
Auxiliary Space: O(m) The frequency map and the vector to store frequencies both use O(m) space.