Remove Abundant Characters

You are given a string s of lowercase English characters and an integer k. You have to remove all characters that occur k or more times in the string.

Return the final string.

Note: The final string can be empty.

Example 1:

Input: s = "aadbbcccca", k = 3

Output: "dbb"

Explanation:

Character frequencies in s:

  • 'a' appears 3 times
  • 'd' appears 1 time
  • 'b' appears 2 times
  • 'c' appears 4 times

'a' and 'c' appear k or more times. Hence 'a' and 'c' will be removed from the string.

Final string = "dbb".

Example 2:

Input: s = "xyz", k = 2

Output: "xyz"

Explanation:

All characters ('x', 'y', 'z') appear exactly once, which is fewer than 2. Thus the whole string is returned.

Now Your Turn!

Pick the correct output for the given input

Input: s="aabbcc", k=3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 100
  • s contains only lowercase English characters
  • 1 <= k <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string removeAbundantCharacters(string s, int k) {
// Your code goes here
}
};
 
Test Case

Input:

K
S