125. 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.

  1. class Solution(object):
  2. def isPalindrome(self, s):
  3. """
  4. :type s: str
  5. :rtype: bool
  6. """
  7. import re
  8. s = re.sub(r"\W", r"", s.lower())
  9. return s == s[::-1]






posted @ 2017-08-08 23:35  xiejunzhao  阅读(250)  评论(0)    收藏  举报