Undo counter: hints, no solution

Easy · Stack

A note-taking app records what a user did as a list of commands. The command add followed by a space and a word appends that word to the note. The command undo removes the word that was added most recently and is still in the note. Undone words are gone for good; there is no redo.

Run all the commands from an empty note and return the final text: the remaining words in the order they were added, separated by single spaces. If no words remain, return the empty string. An undo issued when the note is empty does nothing.

Examples

Input
commands = ["add hello", "add big", "add world", "undo"]
Output
"hello big"
Note
The newest word, world, is removed, leaving hello and big.
Input
commands = ["add a", "undo", "undo", "add b"]
Output
"b"
Note
The first undo empties the note and the second finds nothing to remove.

Constraints

  • 0 <= commands.length <= 2000
  • Every command is undo or add followed by one space and a word of 1 to 8 lowercase letters or digits

Hint 1: a nudge

When an undo arrives, which earlier action does it cancel? Does that depend on how many commands came before it?

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