Reads the Same Both Ways

EasyTwo Pointers≈ 15 min

A phrase reads the same both ways when its letters and digits, read from the end back to the start, spell the same thing as read from the start. Upper and lower case count as the same, and every other character (spaces, punctuation) is ignored.

Given a string phrase, return true if it reads the same both ways, and false if not. A phrase with no letters or digits at all does.

Examples

text
phrase = "Never odd or even"   →  true
phrase = "Step on no pets!"    →  true
phrase = "Top spot, 2 go"      →  false
phrase = "12:21"               →  true

The first phrase, with the spaces dropped and lower-cased, is "neveroddoreven", which is the same reversed. In the third, the 2 has nothing to match on the other side.

Constraints

  • Between 0 and 50,000 characters. Letters are a–z and A–Z, digits 0–9; anything else is ignored.
  • Aim for O(n) time and O(1) extra memory: compare characters in place.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 10 on the judge, 6 of them hidden, and measures how your solution scales.