Print a closest string that does not contain adjacent duplicates | C ++ program to print a string with no adjacent duplicates by doing minimum changes to original string ; Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string noAdjacentDup ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = ' a ' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << noAdjacentDup ( s ) ; return 0 ; }