Given a string S containing lowercase English alphabets of length N and an integer K such that K ≤ N. The task is to find the maximum number of non-repeating characters after removing K characters from the string.
Input: S = “geeksforgeeks”, K = 3
Remove 1 occurrences of each g, k and s so the final string is “geeksforee” and the 6 distinct elements are g, k, s, f, o and r
Input: S = “aabbccddeeffgghh”, k = 1
Remove 1 occurrences of any character we will have only one character which will non repeating.
Naive Approach: The naive idea is to delete all possible K characters among the given string and then find the non-repeating characters in all the formed string. Print the maximum among all the count of non-repeating characters.
Time Complexity: O(N!), where N is the length of the given string.
Auxiliary Space: O(N-K)
Efficient Approach: To optimize the above approach,
The idea is to delete K characters in increasing order of frequency whose frequency is at least 2 to get the count of maximum non-repeating characters.
Below are the steps:
- Create a hash table to store the frequency of each element.
- Insert the frequency of each element in a vector V and sort the vector V in increasing order.
- For each element(say currentElement) of vector V find the minimum among K and currentElement – 1 and decrease both K and V[i] by the minimum of the two.
- Repeat the above step until K is non-zero.
- The count of 1s in vector V gives the maximum number of non-repeating characters after deleting K characters.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(26)
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.
- Check if string remains palindrome after removing given number of characters
- Minimum sum of squares of character counts in a given string after removing k characters
- Maximum points covered after removing an Interval
- Maximum distinct elements after removing k elements
- K-th smallest element after removing some integers from natural numbers
- Character replacement after removing duplicates from a string
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Minimum number of distinct elements after removing m items
- Subsequence queries after removing substrings
- Pair of arrays with equal sum after removing exactly one element from each
- Minimize ASCII values sum after removing all occurrences of one character
- Balance a string after removing extra brackets
- Average of remaining elements after removing K largest and K smallest elements from array
- Find the resultant String after replacing X with Y and removing Z
- Reduce the number to minimum multiple of 4 after removing the digits
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Print string after removing all (“10” or “01”) from the binary string
- Check if string is palindrome after removing all consecutive duplicates
- Minimum number of distinct elements after removing M items | Set 2
- Print reverse string after removing vowels
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.