Lexicographically largest possible String after removal of K characters | C # 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
using System ; class GFG { static String largestString ( String num , int k ) { String ans = " " ; foreach ( char i in num . ToCharArray ( ) ) { 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 ; } public static void Main ( String [ ] args ) { String str = " zyxedcba " ; int k = 1 ; Console . Write ( largestString ( str , k ) + " STRNEWLINE " ) ; } }