[leetcode][125]Valid Palindrome
2016-12-27 22:29
295 查看
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
Subscribe to see which companies asked this question
import string
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
valid = set(string.ascii_lowercase+ string.digits)
left = 0
right = len(s) -1
while left < right:
s_l = s[left].lower()
s_r = s[right].lower()
if (s_l in valid) and (s_r in valid):
if s_l == s_r:
left += 1
right -= 1
else:
return False
elif s_l in valid:
right -= 1
elif s_r in valid:
left += 1
else:
left += 1
right -= 1
return True
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.
Subscribe to see which companies asked this question
import string
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
valid = set(string.ascii_lowercase+ string.digits)
left = 0
right = len(s) -1
while left < right:
s_l = s[left].lower()
s_r = s[right].lower()
if (s_l in valid) and (s_r in valid):
if s_l == s_r:
left += 1
right -= 1
else:
return False
elif s_l in valid:
right -= 1
elif s_r in valid:
left += 1
else:
left += 1
right -= 1
return True
相关文章推荐
- 【leetcode c++】125 Valid Palindrome
- 面试笔试杂项积累-leetcode 121-125
- leetcode 125: Valid Palindrome
- LeetCode - 125 - Valid Palindrome
- Leetcode 125 Python
- 【LEETCODE】125-Valid Palindrome
- leetcode-125 Valid Palindrome
- LeetCode 125: Valid Palindrome
- leetcode解题之125 # Valid Palindrome Java版
- [LeetCode]125 Valid Palindrome
- [leetcode-125]Valid Palindrome(c++)
- [LeetCode]题解(python):125-Valid Palindrome
- leetcode 125:Valid Palindrome
- leetcode: (125) Valid Palindrome
- [Leetcode 125, easy] Valid Palindrome
- leetcode 125 Valid Palindrome
- leetcode 125 —— Valid Palindrome
- leetcode-125 Valid Palindrome
- 【LeetCode】125、Valid Palindrome
- [LeetCode 125] Valid Palindrome