AceYourInterviews
Checking sign-in…

13. Valid Palindrome

EasyFiltered two-pointer comparison

Return whether s reads the same forward and backward after ignoring punctuation and spaces and comparing letters without case. Digits are compared exactly.

Punctuation and spaces do not participate in the comparison.

Example 1

Input

s = "Redder!"

Output

true

Explanation: Ignoring punctuation and letter case leaves redder, which reads the same in both directions.

Example 2

Input

s = "robot"

Output

false

Explanation: The remaining letters do not read the same backward.

Example 3

Input

s = "   "

Output

true

Explanation: No letters or digits remain, so there is no mismatching pair.

Constraints

  • 1 <= s.length <= 2 * 105
  • s contains printable ASCII characters only. Compare letters and digits case-insensitively, ignoring punctuation and spaces.
Hints
Hint 1

Write the exact eligible alphabet first.

Hint 2

Can you find the next retained symbol without storing it?

Hint 3

Skip ignored endpoints, compare folded symbols, then move inward.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Skip punctuation and compare without case

"A,b,a!"
true