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 ; If the current character is greater than the given character ; Insert the character before the greater character ; Return the String ; Append the character at the end ; Return the String ; Driver code
function SmallestString ( s , c ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] > c ) { let temp = s ; s = s . substring ( 0 , i ) ; s += c ; s += temp . substring ( i , temp . length ) ; return s ; } } s += c ; return s ; } let S = " " ; let C = ' ' ; document . write ( SmallestString ( S , C ) ) ;