Leetcode-125Valid Palindrome

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.

My Solution:

import re
class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        return ''.join(re.compile(r"[A-Za-z0-9]").findall(s)).lower() == ''.join(re.compile(r"[A-Za-z0-9]").findall(s)).lower()[::-1]

Reference:

import string

class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        remove_punctuation = str.maketrans('', '', string.punctuation)
        clean_s = s.translate(remove_punctuation).lower().replace(' ', '')
        return clean_s[::-1] == clean_s

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,493评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,451评论 3 20
  • Pathname lookup in Linux. This write-up is based on three...
    朔飞阅读 723评论 0 0
  • 我们常常听人说,以前幻想自己的生活应该是什么样子,而现在过上了自己截然相反的生活,自己的不得解~ 最后感叹:生活真...
    多金阅读 359评论 0 0
  • 后天老爸的生日,刚刚决定回去并买了车票,今年是老爸六十六岁生日,老妈早就念叨着,要闺女包六两肉,六十六个饺子,有些...
    皮儿米阅读 90评论 0 0