Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | Java 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
import java . util . * ; class GFG { static int maxChange ( char [ ] str ) { if ( str . length <= 1 ) { return 0 ; } int minChanges = 0 ; for ( int i = 0 ; i < str . length - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } } return minChanges ; } public static void main ( String [ ] args ) { String str = " bbbbbbb " ; System . out . print ( maxChange ( str . toCharArray ( ) ) ) ; } }