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.
- Find value after N operations to remove N characters of string S with given constraints
- Check if a string can be split into substrings starting with N followed by N characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- 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
- Split a given string into substrings of length K with equal sum of ASCII values
- Count of substrings of a given Binary string with all characters same
- Count number of substrings of a string consisting of same characters
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Check if a string can be split into two strings with same number of K-frequent characters
- Remove characters from a numeric string such that string becomes divisible by 8
- Minimum characters to be replaced to remove the given substring
- Count ways to split a Binary String into three substrings having equal count of zeros
- Remove minimum number of characters so that two strings become anagram
- Split the string into minimum parts such that each part is in the another string
- Remove characters from the first string which are present in the second string
- Remove even frequency characters from the string
- Remove odd indexed characters from a given string
- Remove all characters other than alphabets from 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.