Skip to content
Algorithms for Beginners

Problems / Stack

Typing with backspace

easy

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

keys lists every key someone pressed, in order. Letters and spaces type themselves; the character < means they pressed backspace, which deletes the character just before the cursor. Pressing backspace with nothing on the screen does nothing.

Return the text left on the screen at the end.

Examples

  1. Example 1

    Input
    keys = "cat<<ow"
    Output
    "cow"

    Type c, a, t. Two backspaces leave c. Then o, w.

  2. Example 2

    Input
    keys = "hel<llo"
    Output
    "hello"
  3. Example 3

    Input
    keys = "hi<<<<"
    Output
    ""

    The extra backspaces on an empty screen do nothing.

Rules

  • keys has between 0 and 200,000 characters: lowercase letters, spaces and <.
Words on this page, in plain English (3)Open if any word is new to you
String
A piece of text, written between quotes. Each letter in it is a character.
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Key
The label you look something up by in a Map or object, like a word in a dictionary.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function typedText(keys) {
  // Return the text left on the screen.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in stack

  • Learn the stack pattern

    Keep the most recent unfinished thing on top, and finish it first.

  • Stack with minimum

    Medium. Build a stack that can also tell you its smallest item instantly.

  • Postfix calculator

    Medium. Work out a sum written with the operators after the numbers, like 3 4 + 2 *.

  • Tidy a file path

    Medium. Turn a messy folder path with dots and extra slashes into its simplest form.

  • Days until warmer

    Medium. For each day, count how many days you'd wait for a warmer one.

  • Balanced brackets

    Easy. Check whether every bracket in a string is closed by the right kind, in the right order.