Largest string obtained in Dictionary order after deleting K characters | Python3 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
from collections import deque NEW_LINE def largestString ( n , k , sc ) : NEW_LINE INDENT s = [ i for i in sc ] NEW_LINE deq = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( deq ) > 0 and deq [ - 1 ] < s [ i ] and k > 0 ) : NEW_LINE INDENT deq . popleft ( ) NEW_LINE k -= 1 NEW_LINE DEDENT deq . append ( s [ i ] ) NEW_LINE DEDENT st = " " NEW_LINE for c in deq : NEW_LINE INDENT st = st + c NEW_LINE DEDENT return st NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE k = 2 NEW_LINE sc = " ritz " NEW_LINE result = largestString ( n , k , sc ) NEW_LINE print ( result ) NEW_LINE DEDENT