Problem 283016 · easy · Level 02 Linear Data Structures

Puzzle-Page Palindromes

strings · two pointers · filtering

A palindrome reads the same forwards and backwards. Real sentences contain punctuation, spaces and mixed case, so the first job is to decide which characters count.

A puzzle magazine prints readers' sentences that read the same both ways. To check a submission s, the editor first throws away everything that is not a letter or a digit (spaces, punctuation, symbols) and treats capital and small letters as the same; then she asks whether what is left reads the same from either end. Return True if it does and False if not. A submission with nothing left after this clean-up counts as a palindrome.

Examples

Input:  s = "Step on no pets!"
Output: True
Explanation: cleaned, it reads "steponnopets".
Input:  s = "Step on my pets"
Output: False
Input:  s = "?!"
Output: True
Explanation: nothing is left after cleaning.

Constraints

  • 0 <= len(s) <= 10**4
  • s contains printable ASCII characters

Goals

  • Filter a string down to the characters that matter using str methods
  • Compare a sequence from both ends with two indices
Starting Python…