Typing with backspace
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- 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
Example 1
- Input
- keys = "cat<<ow"
- Output
- "cow"
Type c, a, t. Two backspaces leave c. Then o, w.
Example 2
- Input
- keys = "hel<llo"
- Output
- "hello"
Example 3
- Input
- keys = "hi<<<<"
- Output
- ""
The extra backspaces on an empty screen do nothing.
Rules
keyshas 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.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We replay someone's typing. Every character types itself, except <, which is backspace: it deletes the last character on the screen. Backspace on an empty screen does nothing. We return what's left.
2. Solve an example by hand
cat<<ow: type c, a, t, so the screen says "cat". Backspace: "ca". Backspace: "c". Type o, w: "cow".
Backspace always removes the newest character: last in, first out.
3. The slow way
Keep the screen as a string, and on backspace do screen = screen.slice(0, -1). It gives the right answer. But strings can't be changed in place, so every edit makes a new string, and in the worst case the text is copied again and again. A structure built for adding and removing at the end is a better fit.
4. Spot the pattern
Adding at the end, removing from the end: a stack. An array's push and pop are instant, however long the text gets.
5. The stack idea
Push every typed character. On <, pop. At the end, join what's left into a string.
The screen is a stack
keys
screen (stack, top first)
- empty
function typedText(keys) { const screen = []; for (const key of keys) { if (key === "<") { screen.pop(); // does nothing when the screen is empty } else { screen.push(key); } } return screen.join("");}The screen starts empty.
6. The code, line by line
const screen = [];The screen, as a stack of characters.
for (const key of keys) { if (key === "<") { screen.pop(); // does nothing when the screen is emptyBackspace: pop the newest character. On an empty array,
pop()just returnsundefinedand changes nothing, which is exactly the behaviour we want.} else { screen.push(key); }Anything else is typed: push it.
return screen.join("");join("")glues the characters, bottom to top, into the final text.
7. How fast is it?
| String slicing | Stack | |
|---|---|---|
| Time | O(n²) in the worst case | O(n) |
| Extra memory | O(n) | O(n) |
Common mistakes
- Treating
<as a character when the screen is empty, or crashing on it. An empty screen stays empty. - Building the result with
+=in a loop. For one final string,joinon an array is the clear and fast way.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Typing with backspace
Read the 11 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function typedText(keys) { const screen = []; for (const key of keys) { if (key === "<") { screen.pop(); // does nothing when the screen is empty } else { screen.push(key); } } return screen.join("");}