diff --git a/palindrome_checker.py b/palindrome_checker.py index c0fdc30..1fc2ce7 100644 --- a/palindrome_checker.py +++ b/palindrome_checker.py @@ -1,7 +1,23 @@ -# palindrome_checker.py +import re + def is_palindrome(s): - return s == s[::-1] + # Normalize the string: remove non-alphanumeric characters and convert to lowercase + normalized_s = re.sub(r'[^A-Za-z0-9]', '', s).lower() + + # Early exit for trivial cases + if len(normalized_s) <= 1: + return True + + # Check for palindrome + left, right = 0, len(normalized_s) - 1 + while left < right: + if normalized_s[left] != normalized_s[right]: + return False + left += 1 + right -= 1 + + return True if __name__ == "__main__": - word = input("Enter a word: ") + word = input("Enter a word or phrase: ") print(f"{word} is a palindrome: {is_palindrome(word)}")