text: Lexicographically largest possible String after removal of K characters | JavaScript program to implement the above approach ; Final result String ; If the current char exceeds the character at the top of the stack ; Remove from the end of the String ; Decrease k for the removal ; Insert current character ; Perform remaining K deletions from the end of the String ; Return the String ; Driver Code
code: function largestString ( num , k ) { var ans = " " ; var str = num . split ( " " ) ; for ( const i of str ) { while ( ans . length > 0 && ans [ ans . length - 1 ] < i && k > 0 ) { ans = ans . substring ( 0 , ans . length - 1 ) ; k -- ; } ans += i ; } while ( ans . length > 0 && k -- > 0 ) { ans = ans . substring ( 0 , ans . length - 1 ) ; } return ans ; } var str = " " ; var k = 1 ; document . write ( largestString ( str , k ) + " " ) ;