All problems

Undo Editor

Easy · stack

A tiny text editor starts with empty text and runs a list of commands, in order. Each command is one of these:

Every "type" and "delete" command can be undone, even a "delete" that found nothing to remove. Undoing twice in a row undoes two commands, and there is no way to bring an undone command back. A new "type" or "delete" after an "undo" is simply the newest command.

Return the text after all commands have run (an empty string if it is empty).

Examples

Example 1

Input: commands = ["type abc", "delete 1", "type x"]

Output: "abx"

abc becomes ab after the delete, and x is added at the end.

Example 2

Input: commands = ["type ab", "type cd", "undo", "delete 1"]

Output: "a"

The undo removes cd, leaving ab, and the delete then removes the b.

Example 3

Input: commands = ["type hi", "delete 1", "undo", "undo", "undo"]

Output: ""

The first undo restores hi, the second removes the typing, and the third has nothing to undo.

Constraints