A simplified text editor holds a single line of text, starting empty. It supports m operations, each one of:
TYPE s — append the token s (non-empty, lowercase letters and digits only, no spaces) to the end of the current text.DELETE k — remove the last k characters of the current text (it is guaranteed that k does not exceed the current text's length).UNDO — revert the text to the state it was in immediately before the most recent TYPE/DELETE operation that has not already been undone. If there is nothing left to undo, this operation does nothing.REDO — reapply the most recently undone operation. If there is nothing to redo (either nothing has been undone, or a new TYPE/DELETE was performed since the last undo, which discards the redo history), this operation does nothing.Process all m operations in order and report the final text.
Line 1: an integer m.
Next m lines: one operation each, in the form described above.
A single line: the final text (an empty line if the final text is empty).
TYPE s: 1 ≤ length of s ≤ 20, lowercase English letters and digits only.DELETE k: 1 ≤ k ≤ current text length.Example 1
Input
4 TYPE hello TYPE world UNDO TYPE there
Expected
hellothere
Explanation
After typing hello and world the text is 'helloworld'. UNDO reverts to 'hello'. Typing 'there' gives 'hellothere'.
Example 2
Input
3 TYPE abc DELETE 2 UNDO
Expected
abc
Explanation
Typing 'abc' gives 'abc'. DELETE 2 removes the last two characters, giving 'a'. UNDO reverts that deletion, restoring 'abc'.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →