Largest string obtained in Dictionary order after deleting K characters | C ++ program for the above approach ; Function to find the largest string after deleting k characters ; Deque dq used to find the largest string in dictionary after deleting k characters ; Iterate till the length of the string ; Condition for popping characters from deque ; To store the resultant string ; To form resultant string ; Return the resultant string ; Driver code ; Given String ; Function call ; Print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestString ( int n , int k , string s ) { deque < char > deq ; for ( int i = 0 ; i < n ; ++ i ) { while ( deq . size ( ) > 0 && deq . back ( ) < s [ i ] && k > 0 ) { deq . pop_front ( ) ; k -- ; } deq . push_back ( s [ i ] ) ; } string st = " " ; for ( char c : deq ) st = st + c ; return st ; } int main ( ) { int n = 4 ; int k = 2 ; string sc = " ritz " ; string result = largestString ( n , k , sc ) ; cout << result << endl ; return 0 ; }