All problems

Problem 392

Remove K Adjacent Duplicates

Medium · 20 pts · String, Stack

Problem

Repeatedly find K adjacent equal letters in the string and remove all K of them, until no such group exists. Removing a group can join letters from both sides into a new group, which must then be removed too. The final result does not depend on the order of removals. Print the final string, or EMPTY if nothing is left. Keep a stack of (letter, count) pairs to do this in one pass.

Input

First line: the string S of lowercase letters. Second line: integer K.

Output

Print the final string, or EMPTY.

Constraints

1 <= |S| <= 100000; 2 <= K <= 10000

Example input

deeedbbcccbdaa
3

Example output

aa

Explanation

With K = 3, 'deeedbbcccbdaa' loses 'eee' and 'ccc', giving 'ddbbbdaa'; then 'bbb' goes, giving 'dddaa'; then 'ddd' goes, leaving 'aa'.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Remove Adjacent Duplicates Next: Baseball Game