← DSA practice

Valid Palindrome

EasyTwo Pointers

The problem

A phrase is a palindrome if, after lowercasing and removing non-alphanumeric characters, it reads the same forwards and backwards.

Input: one line.
Output: "true" or "false".

Examples

Input:
A man, a plan, a canal: Panama
Output:
true

Constraints

1 <= length <= 2*10^5
Solve it on LeetCode
Show the worked solution

Keep only alphanumeric characters, lowercase them, and compare with the reverse. Time O(n).

s = input()
c = [ch.lower() for ch in s if ch.isalnum()]
print('true' if c == c[::-1] else 'false')