본문 바로가기

DataStructure/Strings

Valid Palindrome

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

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

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false


My solution:

class Solution:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
size_s = len(s)
start = 0
end = size_s - 1

while start < end:
while start < end and not s[start].isalnum():
start += 1

while start < end and not s[end].isalnum():
end -= 1

if s[start].lower() != s[end].lower():
return False

start += 1
end -= 1

return True

check out isalnum() function.

isalnum means alphabet + number

https://www.tutorialspoint.com/python/string_isalnum.htm

'DataStructure > Strings' 카테고리의 다른 글

Implement strStr()  (0) 2018.10.23
String to Integer (atoi)  (0) 2018.10.23
Valid Anagram  (0) 2018.10.23
First Unique Character in a String  (0) 2018.10.22
Reverse Integer  (0) 2018.10.22