zoukankan      html  css  js  c++  java
  • 125. Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/#/description

    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.

    Hint:

    Check if the left char is equal to the right char. Quite straight forward.

    Sol 1:

    def isPalindrome(self, s):
        l, r = 0, len(s)-1
        while l < r:
            while l < r and not s[l].isalnum():
                l += 1
            while l <r and not s[r].isalnum():
                r -= 1
            if s[l].lower() != s[r].lower():
                return False
            l +=1; r -= 1
        return True

    Note:

    1 Remove the capital letters and other punctuations before comparing. Here are some handy functions.

    1) isalnum()

    Return true when the string has at least one char and the string is composed of letters and numbers.

    ex1.

    s = "A man, a plan, a canal: Panama"
    print s.isalnum()  

    ==> False

    ex2.

    s = "AmanplanacanalPanama"
    print s.isalnum()

    ==> True

     

    ex3.

    s = "A man, a plan, a canal: Panama"
    print s[0].isalnum()  

    ==> True

    ex4.

    s = "A man, a plan, a canal: Panama"
    print s[1].isalnum()  

    ==> False

    2) s1 = s1.replace(' ','').lower()

    Remove spaces and lowercase letters. 

    In this case, merely remove spaces and lowercase letters are not enough, because chars like " : " can be included, and it will jeopardize the reversed string.   

    2 Pay attention to the programming style of loops. While loop is used with the iterate condition like left +=1 or right -= 1. While loop is not for loop, so do not expect the pointer to add value itself. 

    3 In order to aviod "Line 11: IndexError: string index out of range", even if the while left < right condition is written in the outer loop, it is suggested to write it again in the inner loop. 

    4 Use != condition.  If the aim is like "break the loop(or return False) when.... and check the rest ". Use != condition.  

    Pseudocode:

    if the condition is not satisfied:

        return False

    pointer += 1

    Only in this way can we make sure the pointer traverses the string. 

    Similar question, if the input string only contains  letters, numbers, or spaces.  i.e. No “ , ”  “ : ” ...

    We can use the following python trick. 

    def solution2(s):
        s= s.replace(" ","").lower()
        return str(s) == str(s)[::-1]
    
    s = "am an apl anac analpanama"
    solution2(s)

    ==> True

    Note:

    1 s[::-1] prints string s in a reverse manner.  

  • 相关阅读:
    【 DCOS 】织云 CMDB 管理引擎技术详解
    腾讯云无服务器云函数架构精解
    深度学习在 CTR 中应用
    一文教你迅速解决分布式事务 XA 一致性问题
    小程序开发工具全新上线
    秦俊:开放 DevOps 敏捷开发套件,助力开发者驰骋云端
    张兴华:云端架构助力企业快速成长
    王磊:AI 时代物流行业的 OCR 应用
    陈杰:无服务器架构,让云端开发更纯粹
    腾讯织云:DevOps 流水线应用平台践行之路
  • 原文地址:https://www.cnblogs.com/prmlab/p/6947100.html
Copyright © 2011-2022 走看看