Given a string str of lowercase alphabets, the task is to remove minimum characters from the given string so that string can be break into 3 substrings str1, str2, and str3 such that each substring can be empty or can contains only characters ‘a’, ‘b’, and ‘c’ respectively.
Input: str = “aaaabaaxccac”
String after removing b, x, and a then, string str become “aaaaaaccc”
Now str1 = “aaaaaa”, str2 = “”, str3 = “ccc”.
The minimum character removed is 3.
Input: str = “baabcbbdcca”
String after removing b, c, d, and a then, string str become “aabbbcc”
Now str1 = “aa”, str2 = “bbb”, str3 = “cc”.
The minimum character removed is 4.
Approach: This problem can be solved using Greedy Approach. We will use three prefix array to make prefix array of characters ‘a’, ‘b’, and ‘c’. Each prefix array will store the count of letter ‘a’, ‘b’, and ‘c’ at any index i respectively. Below are the steps:
- Create three prefix array as:
- prefa[i] represents the count of letter “a” in prefix of length i.
- prefb[i] represents the count of letter “b” in prefix of length i.
- prefc[i] represents the count of letter “c” in prefix of length i.
- In order to delete minimum number of character, the resultant string should be of maximum size.
- The idea is to fix two positions i and j in string, 0 ? i ? j ≤ N, in order to split string into three parts of all possible length and do the following:
- Remove all character except ‘a’ from the prefix, which ends in i, this will be the string str1.
- Remove all character except ‘c’ from the suffix, which starts in j, this will be the string str3.
- Remove all character except ‘b’ which is between positions i and j, this will be the string str2.
- Therefore, the total length of the resultant string is given by:
total length of (str1 + str2 + str3) = (prefa[i]) + (prefb[j] – prefb[i]) + (prefc[n] – prefc[j])
- Subtract the length of the resultant string from the length of the given string str to get the minimum characters to removed.
Below is the implementation of the above approach:
Time Complexity: O(N2), where N is the length of the given string.
Space Complexity: O(N), where N is the length of the given string.
- Count ways to split a Binary String into three substrings having equal count of zeros
- Split string into three palindromic substrings with earliest possible cuts
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Check if a string can be split into substrings starting with N followed by N characters
- Minimum steps to reach end of array under constraints
- Find value after N operations to remove N characters of string S with given constraints
- Find duplicates under given constraints
- Count of strings that can be formed using a, b and c under given constraints
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Split a given string into substrings of length K with equal sum of ASCII values
- Split the string into substrings using delimiter
- Split the binary string into substrings with equal number of 0s and 1s
- Check if a string can be split into even length palindromic substrings
- Check if it is possible to convert one string into another with given constraints
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Check if a string can be split into two strings with same number of K-frequent characters
- Split the string into minimum parts such that each part is in the another 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 email@example.com. 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.