Minimum substring removals required to make all remaining characters of a string same | C # program for 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
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 ) + " STRNEWLINE " ) ; } 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 ( ) ) ; } }