Given a string of lowercase letters and a number K. The task is to reduce it by removing the characters which appears strictly less than K times in the string.
Input : str = "geeksforgeeks", K = 2 Output : geeksgeeks Input : str = "geeksforgeeks", K = 3 Output : eeee
- Create a hash table of 26 indexes, where 0th index representing ‘a’ and 1th index represent ‘b’ and so on to store the frequency of each of the characters in the input string. Initialize this hash table to zero.
- Iterate through the string and increment the frequency of each character in the hash table. That is, hash[str[i]-‘a’]++.
- Now create a new empty string and once again traverse through the input string and append only those characters in the new string whose frequency in the hash table is more than or equal to k and skip those which appears less than k times.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
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 elements that appear strictly less than k times
- Check if max occurring character of one string appears same no. of times in other
- Remove characters that appear more than k times
- Count of times second string can be formed from the characters of first string
- Largest sub-string where all the characters appear at least K times
- Remove characters from a numeric string such that string becomes divisible by 8
- Find the only element that appears b times
- Remove characters from the first string which are present in the second string
- Remove all characters other than alphabets from string
- Remove even frequency characters from the string
- Remove odd frequency characters from the string
- Longest subsequence where every character appears at-least k times
- How to remove all non-alphanumeric characters from a string in Java
- Remove all non-alphabetical characters of a String in Java
- Count all elements in the array which appears at least K times after their first occurrence
- 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
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Remove elements from the array which appear more than k times