Condition for popping characters from deque
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