Minimum substring removals required to make all remaining characters of a string same
Last Updated :
18 May, 2021
Given a string str of length N, the task is to find the minimum number of substrings required to be removed to make all the remaining characters of the string same.
Note: The substring to be removed must not contain the last remaining character in the string.
Examples:
Input: str = “ACBDAB”
Output: 2
Explanation:
Removing the substring { str[1], …, str[3] } modifies str to “AAB”
Removing the substring {str[2] } modifies str to “AA”
Since all characters of str are equal, the required output is 2.
Input: str = “ZBCDEFZ”
Output: 1
Explanation:
Removing the substring { str[1], …, str[5] } modifies str to “ZZ”
Since all characters of str are equal, the required output is 1.
Approach: The idea is to first remove all the consecutive duplicate characters of the string and count the frequency of each distinct character of the string. Finally, remove all the characters of the string except the character having minimum frequency. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperationNeeded(string str)
{
str = string(str.begin(),
unique(str.begin(), str.end()));
int N = str.length();
int res[256] = { 0 };
for ( int i = 0; i < N; ++i) {
res[str[i]] += 1;
}
res[str[0]] -= 1;
res[str[N - 1]] -= 1;
int ans = INT_MAX;
for ( int i = 0; i < N; ++i) {
ans = min(ans, res[str[i]]);
}
cout << (ans + 1) << endl;
}
int main()
{
string str = "ABCDABCDABCDA" ;
minOperationNeeded(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void minOperationNeeded( char [] str)
{
str = modstring(str);
int N = str.length;
int res[] = new int [ 256 ];
for ( int i = 0 ; i < N; ++i) {
res[str[i]] += 1 ;
}
res[str[ 0 ]] -= 1 ;
res[str[N - 1 ]] -= 1 ;
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; ++i) {
ans = Math.min(ans, res[str[i]]);
}
System.out.print((ans + 1 ) + "\n" );
}
private static char [] modstring( char [] str) {
String s = "" ;
boolean b = true ;
for ( int i = 1 ; i < str.length; ++i) {
if (str[i - 1 ] != str[i])
b = true ;
if (b) {
s += str[i- 1 ];
b = false ;
}
}
return s.toCharArray();
}
public static void main(String[] args)
{
String str = "ABCDABCDABCDA" ;
minOperationNeeded(str.toCharArray());
}
}
|
Python3
import re, sys
def minOperationNeeded(s):
d = {}
str = re.sub(r "(.)\1 + " ,'', s)
N = len ( str )
res = [ 0 for i in range ( 256 )]
for i in range (N):
res[ ord ( str [i])] + = 1
res[ ord ( str [ 0 ])] - = 1
res[ ord ( str [N - 1 ])] - = 1
ans = sys.maxsize
for i in range (N):
ans = min (ans, res[ ord ( str [i])])
print ((ans + 1 ))
if __name__ = = '__main__' :
str = "ABCDABCDABCDA"
minOperationNeeded( str )
|
C#
using System;
class GFG{
static void minOperationNeeded( char [] str)
{
str = modstring(str);
int N = str.Length;
int [] res = new int [256];
for ( int i = 0; i < N; ++i)
{
res[str[i]] += 1;
}
res[str[0]] -= 1;
res[str[N - 1]] -= 1;
int ans = Int32.MaxValue;
for ( int i = 0; i < N; ++i)
{
ans = Math.Min(ans, res[str[i]]);
}
Console.WriteLine((ans + 1) + "\n" );
}
private static char [] modstring( char [] str)
{
string s = "" ;
bool b = true ;
for ( int i = 1; i < str.Length; ++i)
{
if (str[i - 1] != str[i])
b = true ;
if (b)
{
s += str[i - 1];
b = false ;
}
}
return s.ToCharArray();
}
public static void Main()
{
string str = "ABCDABCDABCDA" ;
minOperationNeeded(str.ToCharArray());
}
}
|
Javascript
<script>
function minOperationNeeded(str) {
str = modstring(str);
var N = str.length;
var res = new Array(256).fill(0);
for ( var i = 0; i < N; ++i) {
res[str[i].charCodeAt(0)] += 1;
}
res[str[0].charCodeAt(0)] -= 1;
res[str[N - 1].charCodeAt(0)] -= 1;
var ans = 2147483647;
for ( var i = 0; i < N; ++i) {
ans = Math.min(ans, res[str[i].charCodeAt(0)]);
}
document.write(ans + 1 + "<br>" );
}
function modstring(str) {
var s = "" ;
var b = true ;
for ( var i = 1; i < str.length; ++i) {
if (str[i - 1] !== str[i]) b = true ;
if (b) {
s += str[i - 1];
b = false ;
}
}
return s.split( "" );
}
var str = "ABCDABCDABCDA" ;
minOperationNeeded(str.split( "" ));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(256)
Share your thoughts in the comments
Please Login to comment...