- categories: Code, Interview Question, leetcode, Easy
- source: https://leetcode.com/problems/valid-palindrome/description/
- topics: Two Pointers
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s
, return true
if it is a palindrome, or false
otherwise.
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
i = 0
j = len(s)-1
while i < j:
if not s[i].isalnum():
i += 1
continue
if not s[j].isalnum():
j -= 1
continue
if s[i].lower() != s[j].lower():
return False
i += 1
j -= 1
return True