Undo-redo buffer: hints, no solution

Medium · Stack

Build the engine behind a text buffer that keeps a history of typed words. You are given a list of operations to run against an initially empty buffer.

Type followed by a space and a word appends the word and wipes any words that were waiting to be redone. Undo removes the most recently typed word that is still present and keeps it available for redo. Redo brings back the most recently undone word. Undo and redo do nothing when there is nothing to undo or redo. Show records the current content, which is the words in order joined by single spaces (the empty string when empty).

Return the recorded contents, one per show, in the order the shows happened.

Examples

Input
ops = ["type hello", "type world", "show", "undo", "show", "redo", "show"]
Output
["hello world", "hello", "hello world"]
Note
The buffer shows hello world, then hello after the undo, then hello world again after the redo.
Input
ops = ["type a", "undo", "type b", "redo", "show"]
Output
["b"]
Note
After the undo, typing b wipes the waiting a, so the redo has nothing to bring back.

Constraints

  • 0 <= ops.length <= 1500
  • Operations are type followed by a word of 1 to 8 letters, or undo, redo, show
  • The total length of all recorded contents stays under 3500 characters

Hint 1: a nudge

After an undo, what information must you still hold so a redo can bring the word back, and when does that information stop being useful?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first