1672. Undo Redo Typist

Easy1.5 points estimatedStackStringSimulation

A bare-bones text editor stores every key press in the string log. Typing a lowercase letter appends it to the end of the text and erases the whole redo history. The key < is undo: it takes the last character off the text and remembers it for later, or does nothing when the text is empty. The key > is redo: it puts the most recently undone character back at the end of the text, or does nothing when there is nothing to redo.

Start with empty text, process log from left to right and return the final text as a string, which may be empty. Keep the text and the undone characters on two stacks so the whole log is handled in O(n) time and O(n) extra space, where n is the length of log. Here 1 <= n <= 1000 and log contains only lowercase letters, '<' and '>'.

Example 1

Input:
log = "ab<c"
Output:
"ac"
Explanation:

Type a and b, undo the b, then type c, so the text is ac.

Example 2

Input:
log = "abc<<>>>"
Output:
"abc"
Explanation:

After abc two undos leave a, two redos restore abc, and the last redo has nothing to restore.

Example 3

Input:
log = "<>x"
Output:
"x"
Explanation:

Undo and redo on empty text do nothing, then x is typed.

Constraints

1 ≤ log.length ≤ 1000; log consists of lowercase letters, '<' and '>'

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…