Make the string lexicographically smallest and non palindromic by replacing exactly one character | C ++ program to make the string lexicographically smallest non palindromic string by replacing exactly one character ; Function to find the required string ; length of the string ; Iterate till half of the string ; replacing a non ' a ' char with ' a ' ; Check if there is no ' a ' in string we replace last char of string by ' b ' ; If the input is a single character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findStr ( string S ) { int n = S . size ( ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' a ' ) { S [ i ] = ' a ' ; return S ; } } S [ n - 1 ] = ' b ' ; return n < 2 ? " ▁ - 1 ▁ " : S ; } int main ( ) { string str = " a " ; cout << findStr ( str ) << endl ; string str1 = " abccba " ; cout << findStr ( str1 ) << endl ; return 0 ; }