Given a string of lowercase letters, reduce it by removing the characters which appears more than k times in the string.
Input : str = "geeksforgeeks" k = 2 Output : for Input : str = "geeksforgeeks" k = 3 Output : gksforgks
- Create a hash table of 26 indexes, where 0th index represent ‘a’ and 1th index represent ‘b’ and so on. Initialise the hash table to zero.
- Iterate through the string and count increment the frequency the str[i] character in the hash table.
- Now once again traverse through the string and append those characters in the new string whose frequency in the hash table is less than k and skip those which appears more than equal to k.
Time Complexity – O(N)
Below is the implementation of above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Remove characters from a String that appears exactly K times
- Remove characters from string that appears strictly less than K times
- Remove elements that appear strictly less than k times
- Remove elements from the array which appear more than k times
- Largest substring where all characters appear at least K times | Set 2
- Largest sub-string where all the characters appear at least K times
- Count distinct substrings that contain some characters at most k times
- Remove even frequency characters from the string
- Remove odd frequency characters from the string
- Remove all characters other than alphabets from string
- How to remove all non-alphanumeric characters from a string in Java
- Minimum characters to be replaced to remove the given substring
- Remove first adjacent pairs of similar characters until possible
- Remove all non-alphabetical characters of a String in Java
- Remove minimum number of characters so that two strings become anagram
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove minimum characters from string to split it into three substrings under given constraints
- Count of times second string can be formed from the characters of first string
- Remove characters from a numeric string such that string becomes divisible by 8
- Remove characters from the first string which are present in the second string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.