Minimum substring removals required to make all remaining characters of a string same | 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 ( Max Integer value ) ; Iterate over all characters of the String str ; Update ans ; Given String
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 + " " ) ; } 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 = " " ; minOperationNeeded ( str . split ( " " ) ) ;