Editor With Undo and Redo
A text editor starts with empty text. commands is a list of strings applied in order:
"type X"appends the wordXto the text."undo"takes back the most recenttypethat is still in effect, removing its word from the end of the text."redo"puts back the most recently undone word, if there is one that has not been redone yet.
A new type throws away every word that could still be redone. An undo with nothing to undo and a redo with nothing to redo do nothing. Return the final text with the words joined together with no separator, or "" if it is empty.
Example 1
commands ["type ab","type cd","undo","redo"]
returns "abcd"
ab and cd are typed, undo removes cd, and redo puts it back.
Example 2
commands ["type ab","type cd","undo","type ef","redo"]
returns "abef"
After cd is undone, typing ef discards it, so the final redo has nothing to put back.
Constraints
0 <= len(commands) <= 100- each word
Xhas 1 to 5 lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.