LeetCode/NeetCodeRoadmap/Arrays_and_Hashing/125_Valid_Palindrome.py

40 lines
1.0 KiB
Python

import string
import math
def isPalindrome(s):
"""
:type s: str
:rtype: bool
"""
# Create a translation table to remove uppercase letters and special characters
translation_table = str.maketrans("","", string.punctuation + string.whitespace)
# Use the translation table to remove uppercase letters and special characters
upper_string = s.translate(translation_table)
lower_string = upper_string.lower()
half = len(lower_string)/2
# Round the value down since middle value doesn't matter
halfdown = math.floor(half)
# print(halfup)
# print(halfdown)
first = []
second = []
# print(lower_string)
for i in range(halfdown):
# print(i)
# print(lower_string[i])
first.append(lower_string[i])
for j in range(halfdown+1, len(lower_string)):
second.insert(0, lower_string[j])
# print(first)
# print(second)
if first == second:
return True
else:
return False
print(isPalindrome("A man, a plan, a canal: Panama"))