Minimum replacements in a string to make adjacent characters unequal | Function which counts the minimum number of required operations ; n stores the length of the string s ; ans will store the required ans ; i is the current index in the string ; Move j until characters s [ i ] & s [ j ] are equal or the end of the string is reached ; diff stores the length of the substring such that all the characters are equal in it ; We need atleast diff / 2 operations for this substring ; Driver code
function count_minimum ( s ) { var n = s . length ; var ans = 0 ; var i = 0 ; while ( i < n ) { var j = i ; while ( s [ j ] === s [ i ] && j < n ) { j ++ ; } var diff = j - i ; ans += parseInt ( diff / 2 ) ; i = j ; } document . write ( ans + " " ) ; } var str = " " ; count_minimum ( str ) ;