Problem: Undo/Redo for a Simple Text Editor
Design a data structure that simulates a very small text editor with undo and redo.
You are given a sequence of commands; after each command, output the current text.
Commands
-
TYPE s
: append string
s
to the end of the current text.
-
DELETE k
: delete the last
k
characters (if
k
exceeds current length, delete everything).
-
UNDO
: undo the most recent
mutating
command (
TYPE
or
DELETE
) that has not already been undone.
-
REDO
: redo the most recent undone command, if no new mutating command has occurred since the undo.
Notes / Rules
-
UNDO
/
REDO
that cannot be applied should do nothing.
-
Any new
TYPE
/
DELETE
after an
UNDO
clears the redo history.
Input / Output
-
Input:
a list of commands in the format above.
-
Output:
the editor text after each command.
Example
Commands:
-
TYPE abc
-
TYPE d
-
DELETE 2
-
UNDO
-
REDO
Outputs:
-
abc
-
abcd
-
ab
-
abcd
-
ab
Constraints (typical)
-
Up to
2×105
commands.
-
Total typed characters across all
TYPE
commands up to
2×105
.