Given a string S of length N, consisting of lowercase English alphabets only, the task is to find the minimum possible length of run-length-encoding that can be generated by removing at most K characters from the string S.
Input: S = “abbbcdcdd”, N = 9, K = 2
Explanation: One possible way is to delete both occurrences of ‘c’ from S.
The new string generated is “abbbddd” whose run-length-encoding is “ab3d3”.
Therefore, the length of the encoded string is 5.
Input: S = “aabbca”, N = 6, K = 3
Explanation: One possible way is to delete both the occurrences of ‘b’ and one occurrence of ‘c’.
The new string generated is “aaa” whose run-length-encoding is “a3”.
Therefore, the length of the encoded string is 2
Naive Approach: The simplest approach to solve the problem is to remove every combination of K characters from the string and calculate their respective run-length-encoding. Finally, print the length of the smallest run-length-encoding obtained.
Time Complexity: O(K * N!(N – K)! * K!)
Auxiliary Space: O(K)
Efficient Approach: To optimize the above approach, follow the steps below to solve the problem:
- Maintain an auxiliary array dp[n][k][n], where dp[idx][K][last][count] denotes the minimum run-length-encoding obtained starting from index idx where, K denotes the number of deletions remaining, last denotes the last character with frequency count so far.
- For every character, two possibilities exists, either to delete the character or to retain it.
- Consider that the current character at index idx is deleted and calculate recursively the minimum run-length encoding obtained by passing the parameters (idx + 1, K – 1, last, count)
- Now, consider that the current character at index idx is retained and calculate recursively the minimum run-length encoding for the following two cases:
- If S[idx] = last: Calculate minimum run-length encoding by passing the parameters (idx + 1, K, S[idx], count + 1).
- Otherwise, calculate minimum run-length encoding by passing the parameters (idx + 1, K, S[idx], 1).
- Return the minimum of the above-computed values and repeat the above steps for all indices of the string.
Below is the implementation of the above approach:
Time Complexity: O(26 * N2 * K)
Auxiliary Space: O(26 * N2 * K)
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.
- Run Length Encoding in Python
- Run Length Encoding
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Minimum sum of squares of character counts in a given string after removing k characters
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Maximum non-repeating characters after removing K characters
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Lexicographically smallest string formed by removing at most one character
- Find minimum possible size of array with given rules for removing elements
- Check if string remains palindrome after removing given number of characters
- Minimize cost to empty a given string by removing characters alphabetically
- Lexicographically smallest subsequence possible by removing a character from given string
- Ways of transforming one string to other by removing 0 or more characters
- Reduce the string by removing K consecutive identical characters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Maximum sum subarray removing at most one element
- Check if an encoding represents a unique binary string
- Minimum characters to be replaced to make a string concatenation of a K-length palindromic 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.