Lexicographically smallest string possible by inserting given character | Function to obtain lexicographically smallest string possible by inserting character c in the string s ; Traverse the string ; Check if current character is greater than the given character ; Insert the character before the first greater character ; Return the string ; Append the character at the end ; Return the string ; Function call
def SmallestString ( s , c ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < len ( s ) ) : NEW_LINE INDENT if s [ i ] > c : NEW_LINE INDENT s = s [ : i ] + c + s [ i : ] NEW_LINE return s NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT s = s + c NEW_LINE return s NEW_LINE DEDENT S = ' abd ' NEW_LINE C = ' c ' NEW_LINE print ( SmallestString ( S , C ) ) NEW_LINE