EasyTwo PointersString

1 approach, code in every language

Problem description

You are given a string that may contain letters, digits, spaces, and punctuation. The task is to decide whether the string reads the same forward and backward once you ignore everything that is not a letter or a digit, and once you treat uppercase and lowercase letters as equal.

Return true when the cleaned-up version of the string is a palindrome, and false otherwise. An empty string (after filtering) counts as a palindrome.

Example 1

Input: s = "A man, a plan, a canal: Panama"

Output: true

After removing punctuation and spaces and lowercasing, the text becomes "amanaplanacanalpanama", which mirrors itself.

Example 2

Input: s = "race a car"

Output: false

The filtered text "raceacar" differs when read backward, so it is not a palindrome.

Constraints

  • 1 ≤ s.length ≤ 2 * 10^5
  • s consists only of printable ASCII characters.

After the pattern

Now solve one you haven't seen.

Mirror Phrase Check is our own problem on the technique behind Valid Palindrome: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free