Count minimum substring removals required to reduce string to a single distinct character
Last Updated :
09 Jun, 2021
Given a string S consisting of ‘X’, ‘Y’ and ‘Z’ only, the task is to convert S to a string consisting of only a single distinct character by selecting a character and removing substrings that does not contain that character, minimum number of times.
Note: Once a character is chosen, no other character can be used in further operations.
Examples:
Input: S = “XXX”
Output: 0
Explanation: Since the given string already consists of a single distinct character, i.e. X, no removal is required. Therefore, the required count is 0.
Input: S = “XYZXYZX”
Output: 2
Explanation:
Selecting the character ‘X’ and removing the substrings “YZ” in two consecutive operations reduces the string to “XXX”, which consists of a single distinct character only.
Approach: The idea is to count occurrences of each character using unordered_map and count the number of removals required for each of them and print the minimum. Follow the steps below to solve the problem:
- Initialize an unordered_map and store the indices of occurrences for each of the characters.
- Iterate over all the characters of the string S and update occurrences of the characters ‘X’, ‘Y’ and ‘Z’ in the map.
- Iterate over the Map and for each character, count the number of removals required for each character.
- After calculating for each character, print the minimum count obtained for any of the characters.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations(string s, int n)
{
unordered_map< char , vector< int > > mp;
for ( int i = 0; i < n; i++) {
mp[s[i]].push_back(i);
}
int ans = INT_MAX;
for ( auto x : mp) {
int curr = 0;
int prev = 0;
bool first = true ;
for ( int index : (x.second)) {
if (first) {
if (index > 0) {
curr++;
}
prev = index;
first = false ;
}
else {
if (index != prev + 1) {
curr++;
}
prev = index;
}
}
if (prev != n - 1) {
curr++;
}
ans = min(ans, curr);
}
cout << ans;
}
int main()
{
string s = "YYXYZYXYZXY" ;
int N = s.length();
minimumOperations(s, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void minimumOperations(String s, int n)
{
HashMap<Character, List<Integer>> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(s.charAt(i)))
{
mp.get(s.charAt(i)).add(i);
}
else
{
mp.put(s.charAt(i), new ArrayList<Integer>(Arrays.asList(i)));
}
}
int ans = Integer.MAX_VALUE;
for (Map.Entry<Character, List<Integer>> x : mp.entrySet())
{
int curr = 0 ;
int prev = 0 ;
boolean first = true ;
for (Integer index : (x.getValue()))
{
if (first)
{
if (index > 0 )
{
curr++;
}
prev = index;
first = false ;
}
else
{
if (index != prev + 1 )
{
curr++;
}
prev = index;
}
}
if (prev != n - 1 )
{
curr++;
}
ans = Math.min(ans, curr);
}
System.out.print(ans);
}
public static void main(String[] args)
{
String s = "YYXYZYXYZXY" ;
int N = s.length();
minimumOperations(s, N);
}
}
|
Python3
import sys;
INT_MAX = sys.maxsize;
def minimumOperations(s, n) :
mp = {};
for i in range (n) :
if s[i] in mp :
mp[s[i]].append(i);
else :
mp[s[i]] = [i];
ans = INT_MAX;
for x in mp :
curr = 0 ;
prev = 0 ;
first = True ;
for index in mp[x] :
if (first) :
if (index > 0 ) :
curr + = 1 ;
prev = index;
first = False ;
else :
if (index ! = prev + 1 ) :
curr + = 1 ;
prev = index;
if (prev ! = n - 1 ) :
curr + = 1 ;
ans = min (ans, curr);
print (ans);
if __name__ = = "__main__" :
s = "YYXYZYXYZXY" ;
N = len (s);
minimumOperations(s, N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void minimumOperations( string s, int n)
{
Dictionary< char ,
List< int >> mp = new Dictionary< char ,
List< int >>();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(s[i]))
{
mp[s[i]].Add(i);
}
else
{
mp[s[i]] = new List< int >();
mp[s[i]].Add(i);
}
}
int ans = Int32.MaxValue;
foreach (KeyValuePair< char , List< int >> x in mp)
{
int curr = 0;
int prev = 0;
bool first = true ;
foreach ( int index in (x.Value))
{
if (first)
{
if (index > 0)
{
curr++;
}
prev = index;
first = false ;
}
else
{
if (index != prev + 1)
{
curr++;
}
prev = index;
}
}
if (prev != n - 1)
{
curr++;
}
ans = Math.Min(ans, curr);
}
Console.Write(ans);
}
static void Main()
{
string s = "YYXYZYXYZXY" ;
int N = s.Length;
minimumOperations(s, N);
}
}
|
Javascript
<script>
function minimumOperations(s, n)
{
var mp = {};
for ( var i = 0; i < n; i++) {
if (mp.hasOwnProperty(s[i])) {
mp[s[i]].push(i);
} else {
mp[s[i]] = [];
mp[s[i]].push(i);
}
}
var ans = 2147483647;
for (const [key, value] of Object.entries(mp)) {
var curr = 0;
var prev = 0;
var first = true ;
for (const index of value) {
if (first) {
if (index > 0) {
curr++;
}
prev = index;
first = false ;
} else {
if (index !== prev + 1) {
curr++;
}
prev = index;
}
}
if (prev !== n - 1) {
curr++;
}
ans = Math.min(ans, curr);
}
document.write(ans);
}
var s = "YYXYZYXYZXY" ;
var N = s.length;
minimumOperations(s, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...