Skip to the content

The Pompeii graffito

Problem

Lucius paints notices on the walls of Pompeii, and a neighbor bet him he couldn't leave something on a wall that read the same forwards and backwards. Lucius isn't going to paint anything new: he'll erase letters from a graffito that is already there, without moving the ones that stay, until what's left reads the same both ways. Every erased letter is work, so he wants to erase as few as possible.

You get text, the letters of the graffito, lowercase and with no spaces (0 to 12 letters). You can erase letters from anywhere, not just the ends, and the ones that stay keep their order. Return, as a whole number, the fewest letters you have to erase so that what's left reads the same backwards. An empty text or a single letter already reads the same backwards.

With "statues" you erase the u and the e, "stats" is left, and you return 2. With "that" erasing either the h or the a is enough: you return 1. With "radar" there is nothing to erase: you return 0.

Examples

  • The first example

    "statues" → 2

  • The second example

    "that" → 1

  • It already reads the same

    "radar" → 0

  • The extra letter is at the end

    "aab" → 1

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

You start with this

Python

def min_erase(text):
    pass

JavaScript

function minErase(text) {
}
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(2ⁿ) challenges

See all challenges →