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.
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.
UNDO
/
REDO
that cannot be applied should do nothing.
TYPE
/
DELETE
after an
UNDO
clears the redo history.
Commands:
TYPE abc
TYPE d
DELETE 2
UNDO
REDO
Outputs:
abc
abcd
ab
abcd
ab
TYPE
commands up to
.