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
using System ; class GFG { static String findStr ( char [ ] S ) { int n = S . Length ; for ( int i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' a ' ) { S [ i ] = ' a ' ; return new String ( S ) ; } } S [ n - 1 ] = ' b ' ; return n < 2 ? " ▁ - 1 ▁ " : new String ( S ) ; } public static void Main ( String [ ] args ) { String str = " a " ; Console . WriteLine ( findStr ( str . ToCharArray ( ) ) ) ; String str1 = " abccba " ; Console . WriteLine ( findStr ( str1 . ToCharArray ( ) ) ) ; } }