LeetCode Valid Palindrome Python

Valid Palindrome

 

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

 

class Solution:
    # @param s, a string
    # @return a boolean
    def isPalindrome(self, s):
        ls = [i.lower() for i in s if i.isalnum()]
        l = len(ls)
        tmp1 = ls[:l/2]
        tmp2 = list(reversed(ls[l/2:] if l%2==0 else ls[l/2+1:]))
        
        return True if tmp1==tmp2 else False

 

posted @ 2015-03-28 19:02  繁星苑  阅读(189)  评论(0编辑  收藏  举报