Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxChange ( string str ) { if ( str . size ( ) <= 1 ) { return 0 ; } int minChanges = 0 ; for ( int i = 0 ; i < str . size ( ) - 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 ; } int main ( ) { string str = " bbbbbbb " ; cout << maxChange ( str ) ; return 0 ; }