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:
"type <text>": add<text>to the end of the text.<text>is a word of lowercase letters, for example"type hello"."delete <k>": remove the lastkcharacters. If the text has fewer thankcharacters, remove all of them."undo": reverse the most recent"type"or"delete"command that has not already been undone, putting the text back exactly as it was before that command. If there is nothing left to undo, do nothing.
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
- 1 <= len(commands) <= 10^4
- Every command is type <text>, delete <k> or undo.
- <text> has 1 to 20 lowercase letters, and exactly one space follows the command name.
- 1 <= k <= 10^4