Remove minimum characters from string to split it into three substrings under given constraints
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.
Example:
Input: str = “aaaabaaxccac”
Output: 3
Explanation:
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”
Output: 4
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
void min_remove(string str)
{
int N = str.length();
int prefix_a[N + 1];
int prefix_b[N + 1];
int prefix_c[N + 1];
prefix_a[0] = 0;
prefix_b[0] = 0;
prefix_c[0] = 0;
for ( int i = 1; i <= N; i++) {
prefix_a[i]
= prefix_a[i - 1]
+ (str[i - 1] == 'a' );
prefix_b[i]
= prefix_b[i - 1]
+ (str[i - 1] == 'b' );
prefix_c[i]
= prefix_c[i - 1]
+ (str[i - 1] == 'c' );
}
int maxi = INT_MIN;
for ( int i = 0; i <= N; i++) {
for ( int j = i; j <= N; j++) {
maxi = max(maxi,
(prefix_a[i]
+ (prefix_b[j]
- prefix_b[i])
+ (prefix_c[N]
- prefix_c[j])));
}
}
cout << (N - maxi) << endl;
}
int main()
{
string str = "aaaabaaxccac" ;
min_remove(str);
return 0;
}
|
Java
class GFG{
static void min_remove(String str)
{
int N = str.length();
int []prefix_a = new int [N + 1 ];
int []prefix_b = new int [N + 1 ];
int []prefix_c = new int [N + 1 ];
prefix_a[ 0 ] = 0 ;
prefix_b[ 0 ] = 0 ;
prefix_c[ 0 ] = 0 ;
for ( int i = 1 ; i <= N; i++)
{
prefix_a[i] = prefix_a[i - 1 ] +
( int )((str.charAt(
i - 1 ) == 'a' ) ? 1 : 0 );
prefix_b[i] = prefix_b[i - 1 ] +
( int )((str.charAt(i - 1 ) ==
'b' ) ? 1 : 0 );
prefix_c[i] = prefix_c[i - 1 ] +
( int )((str.charAt(i - 1 ) ==
'c' ) ? 1 : 0 );
}
int maxi = Integer.MIN_VALUE;
for ( int i = 0 ; i <= N; i++)
{
for ( int j = i; j <= N; j++)
{
maxi = Math.max(maxi, (prefix_a[i] +
(prefix_b[j] -
prefix_b[i]) +
(prefix_c[N] -
prefix_c[j])));
}
}
System.out.println((N - maxi));
}
public static void main(String []args)
{
String str = "aaaabaaxccac" ;
min_remove(str);
}
}
|
Python3
import sys
def min_remove(st):
N = len (st)
prefix_a = [ 0 ] * (N + 1 )
prefix_b = [ 0 ] * (N + 1 )
prefix_c = [ 0 ] * (N + 1 )
prefix_a[ 0 ] = 0
prefix_b[ 0 ] = 0
prefix_c[ 0 ] = 0
for i in range ( 1 , N + 1 ):
if (st[i - 1 ] = = 'a' ):
prefix_a[i] = (prefix_a[i - 1 ] + 1 )
else :
prefix_a[i] = prefix_a[i - 1 ]
if (st[i - 1 ] = = 'b' ):
prefix_b[i] = (prefix_b[i - 1 ] + 1 )
else :
prefix_b[i] = prefix_b[i - 1 ]
if (st[i - 1 ] = = 'c' ):
prefix_c[i] = (prefix_c[i - 1 ] + 1 )
else :
prefix_c[i] = prefix_c[i - 1 ]
maxi = - sys.maxsize - 1 ;
for i in range ( N + 1 ):
for j in range (i, N + 1 ):
maxi = max (maxi,
(prefix_a[i] +
(prefix_b[j] -
prefix_b[i]) +
(prefix_c[N] -
prefix_c[j])))
print ((N - maxi))
if __name__ = = "__main__" :
st = "aaaabaaxccac"
min_remove(st)
|
C#
using System;
class GFG{
static void min_remove( string str)
{
int N = str.Length;
int []prefix_a = new int [N + 1];
int []prefix_b = new int [N + 1];
int []prefix_c = new int [N + 1];
prefix_a[0] = 0;
prefix_b[0] = 0;
prefix_c[0] = 0;
for ( int i = 1; i <= N; i++)
{
prefix_a[i] = prefix_a[i - 1] +
( int )((str[i - 1] == 'a' ) ?
1 : 0);
prefix_b[i] = prefix_b[i - 1] +
( int )((str[i - 1] == 'b' ) ?
1 : 0);
prefix_c[i] = prefix_c[i - 1] +
( int )((str[i - 1] == 'c' ) ?
1 : 0);
}
int maxi = Int32.MinValue;
for ( int i = 0; i <= N; i++)
{
for ( int j = i; j <= N; j++)
{
maxi = Math.Max(maxi, (prefix_a[i] +
(prefix_b[j] -
prefix_b[i]) +
(prefix_c[N] -
prefix_c[j])));
}
}
Console.WriteLine((N - maxi));
}
public static void Main()
{
string str = "aaaabaaxccac" ;
min_remove(str);
}
}
|
Javascript
<script>
function min_remove(str)
{
let N = str.length;
let prefix_a = Array.from({length: N + 1}, (_, i) => 0);
let prefix_b = Array.from({length: N + 1}, (_, i) => 0);
let prefix_c = Array.from({length: N + 1}, (_, i) => 0);
prefix_a[0] = 0;
prefix_b[0] = 0;
prefix_c[0] = 0;
for (let i = 1; i <= N; i++)
{
prefix_a[i] = prefix_a[i - 1] +
((str[
i - 1] == 'a' ) ? 1 : 0);
prefix_b[i] = prefix_b[i - 1] +
((str[i - 1] ==
'b' ) ? 1 : 0);
prefix_c[i] = prefix_c[i - 1] +
((str[i - 1] ==
'c' ) ? 1 : 0);
}
let maxi = Number.MIN_VALUE;
for (let i = 0; i <= N; i++)
{
for (let j = i; j <= N; j++)
{
maxi = Math.max(maxi, (prefix_a[i] +
(prefix_b[j] -
prefix_b[i]) +
(prefix_c[N] -
prefix_c[j])));
}
}
document.write((N - maxi));
}
let str = "aaaabaaxccac" ;
min_remove(str);
</script>
|
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.
Last Updated :
07 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...