Minimum substring removals required to make all remaining characters of a string same | C ++ program to implement the above approach ; Function to count minimum operations required to make all characters equal by repeatedly removing substring ; Remove consecutive duplicate characters from str ; Stores length of the string ; Stores frequency of each distinct characters of the string str ; Iterate over all the characters of the string str ; Update frequency of str [ i ] ; Decrementing the frequency of the string str [ 0 ] ; Decrementing the frequency of the string str [ N - 1 ] ; Stores the required count ; Iterate over all characters of the string str ; Update ans ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE 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 ; }