Find a string in lexicographic order which is in between given two strings | Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to 'a ; Driver Code ; If not equal , print the resultant string
def lexNext ( s , n ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if s [ i ] != ' z ' : NEW_LINE INDENT k = ord ( s [ i ] ) NEW_LINE s [ i ] = chr ( k + 1 ) NEW_LINE return ' ' . join ( s ) NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT s [ i ] = ' a ' NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abcdeg " NEW_LINE T = " abcfgh " NEW_LINE n = len ( S ) NEW_LINE S = list ( S ) NEW_LINE res = lexNext ( S , n ) NEW_LINE if res != T : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT