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
def findStr ( S ) : NEW_LINE INDENT S = list ( S ) NEW_LINE n = len ( S ) NEW_LINE for i in range ( 0 , n // 2 ) : NEW_LINE DEDENT ' NEW_LINE INDENT if S [ i ] != ' a ' : NEW_LINE INDENT S [ i ] = ' a ' NEW_LINE return ( ' ' . join ( S ) ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT S [ n - 1 ] = ' b ' NEW_LINE if n < 2 : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT else : NEW_LINE INDENT return ( ' ' . join ( S ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = ' a ' NEW_LINE print ( findStr ( str1 ) ) NEW_LINE str2 = ' abccba ' NEW_LINE print ( findStr ( str2 ) ) NEW_LINE DEDENT