Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | JavaScript Program to implement the above approach Function to count the changes required such that no palindromic subString of length exceeding 1 is present in the String ; Base Case ; Stores the count ; Iterate over the String ; Palindromic SubString of Length 2 ; Replace the next character ; Increment changes ; Palindromic SubString of Length 3 ; Replace the next character ; Increment changes ; Driver Code
function maxChange ( str ) { if ( str . length <= 1 ) { return 0 ; } var minChanges = 0 ; for ( var i = 0 ; i < str . length - 1 ; i ++ ) { if ( str [ i ] === str [ i + 1 ] ) { str [ i + 1 ] = " " ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] === str [ i + 1 ] ) { str [ i + 1 ] = " " ; minChanges += 1 ; } } return minChanges ; } var str = " " ; document . write ( maxChange ( str . split ( " " ) ) ) ;