From 05ba99b479fd0d96e2652148ca5ca77e354eba24 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Wed, 30 Aug 2023 08:51:55 -0700 Subject: [PATCH] solution works, but maketrans from string library not accepted --- .../125_Valid_Palindrome.py | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/NeetCodeRoadmap/Arrays_and_Hashing/125_Valid_Palindrome.py b/NeetCodeRoadmap/Arrays_and_Hashing/125_Valid_Palindrome.py index f2f712b..69d5ebe 100644 --- a/NeetCodeRoadmap/Arrays_and_Hashing/125_Valid_Palindrome.py +++ b/NeetCodeRoadmap/Arrays_and_Hashing/125_Valid_Palindrome.py @@ -1,4 +1,5 @@ import string +import math def isPalindrome(s): """ @@ -6,17 +7,34 @@ def isPalindrome(s): :rtype: bool """ # Create a translation table to remove uppercase letters and special characters - translation_table = str.maketrans("", "", string.ascii_uppercase + string.punctuation + string.whitespace) + translation_table = str.maketrans("","", string.punctuation + string.whitespace) # Use the translation table to remove uppercase letters and special characters - cleaned_string = s.translate(translation_table) - - half = len(cleaned_string)/2 + 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 = [] - for i in range(len(half)): - - return cleaned_string + # 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")) \ No newline at end of file