Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Given String S ; Function Call
function firstSubsequence ( s ) { let isMax = - 1 ; for ( let i = 0 ; i < s . length - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s = s . substring ( 0 , isMax ) + s . substring ( isMax + 1 ) ; s . rerase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; s = s . substring ( 0 , s . length - 1 ) ; } document . write ( s ) ; } let S = " " ; firstSubsequence ( S ) ;