Make the string lexicographically smallest and non palindromic 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
function findStr ( S ) { var n = S . length ; for ( var i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' ' ) { S [ i ] = ' ' ; return S . join ( " " ) ; ; } } S [ n - 1 ] = ' ' ; return n < 2 ? " " : S . join ( " " ) ; ; } var str = " " ; document . write ( findStr ( Array . from ( str ) ) + " " ) ; var str1 = " " ; document . write ( findStr ( Array . from ( str1 ) ) ) ;