Lexicographically smallest string formed by removing at most one character | Function to return the smallest String ; iterate the String ; first point where s [ i ] > s [ i + 1 ] ; append the String without i - th character in it ; leave the last character ; Driver Code
function smallest ( s ) { let l = s . length ; let ans = " " ; for ( let i = 0 ; i < l - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { for ( let j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s [ j ] ; } } return ans ; } } ans = s . substring ( 0 , l - 1 ) ; return ans ; } let s = " " ; document . write ( smallest ( s ) ) ;