Skip to the content

The keyboard with backspace

Problem

Sofia types on the bus, with one finger and a lot of bumps. The keyboard saves every key she presses, in order, including backspace, which is written down as #.

You get that log, a string that may be empty. Return what is left written at the end: each # erases the last character still written at that moment, and is never written itself. A # when nothing is written does nothing. Spaces are characters like any other.

With "cag#t" you return "cat": the g is erased and then the t comes. With "ab##c" you return "c", and with "##hello" you return "hello".

Examples

  • Fixes one letter

    "cag#t" → "cat"

  • Erases two in a row

    "ab##c" → "c"

  • Erasing over nothing

    "##hello" → "hello"

  • No keys

    "" → ""

  • With spaces

    "see you latre##er" → "see you later"

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def final_text(keys):
    pass

JavaScript

function finalText(keys) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →