|
| 1 | +''' |
| 2 | +125. Valid Palindrome |
| 3 | +Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. |
| 4 | +A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. |
| 5 | + |
| 6 | +Example 1: |
| 7 | +Input: s = "A man, a plan, a canal: Panama" |
| 8 | +Output: true |
| 9 | +Explanation: "amanaplanacanalpanama" is a palindrome. |
| 10 | + |
| 11 | +Example 2: |
| 12 | +Input: s = "race a car" |
| 13 | +Output: false |
| 14 | +Explanation: "raceacar" is not a palindrome. |
| 15 | +''' |
| 16 | + |
| 17 | +# Brute Force |
| 18 | +# Time Complexity: O(n) |
| 19 | +# Space Complexity: O(n) |
| 20 | + |
| 21 | +''' |
| 22 | +Below is the code for the brute force approach. |
| 23 | +It uses the built-in string methods to clean the input string and check if it is a palindrome. |
| 24 | +''' |
| 25 | + |
| 26 | +class Solution: |
| 27 | + def isPalindrome(self, s): |
| 28 | + clean = ''.join(c for c in s if c.isalnum()).lower() |
| 29 | + return clean == clean[::-1] |
| 30 | + |
| 31 | +# Two Pointers |
| 32 | +# Time Complexity: O(n) |
| 33 | +# Space Complexity: O(n) |
| 34 | + |
| 35 | +''' |
| 36 | +Below is the code for the two pointers approach. |
| 37 | +It uses two pointers to check if the cleaned string is a palindrome. |
| 38 | +The two pointers start at the beginning and end of the string and move towards the center, comparing characters along the way. |
| 39 | +If any characters don't match, it returns False. If all characters match, it returns True. |
| 40 | +''' |
| 41 | + |
| 42 | +class Solution: |
| 43 | + def isPalindrome(self, s): |
| 44 | + clean = ''.join(c for c in s if c.isalnum()).lower() |
| 45 | + i=0 |
| 46 | + j=len(clean)-1 |
| 47 | + |
| 48 | + while i<=j: |
| 49 | + if clean[i]!=clean[j]: |
| 50 | + return False |
| 51 | + else: |
| 52 | + i+=1 |
| 53 | + j-=1 |
| 54 | + return True |
| 55 | + |
| 56 | +# Two Pointers (Optimized) |
| 57 | +# Time Complexity: O(n) |
| 58 | +# Space Complexity: O(1) |
| 59 | + |
| 60 | +''' |
| 61 | +This approach uses the two-pointer technique to efficiently check if a string is a palindrome |
| 62 | +while ignoring non-alphanumeric characters and treating uppercase and lowercase letters as the same |
| 63 | +''' |
| 64 | + |
| 65 | +class Solution: |
| 66 | + def isPalindrome(self, s): |
| 67 | + i=0 |
| 68 | + j=len(s)-1 |
| 69 | + |
| 70 | + while i<j: |
| 71 | + while i<j and not s[i].isalnum(): |
| 72 | + i+=1 |
| 73 | + while i<j and not s[j].isalnum(): |
| 74 | + j-=1 |
| 75 | + |
| 76 | + if s[i].lower()!=s[j].lower(): |
| 77 | + return False |
| 78 | + |
| 79 | + i+=1 |
| 80 | + j-=1 |
| 81 | + return True |
| 82 | + |
| 83 | +sol = Solution() |
| 84 | +s = "Was it a car or a cat I saw?" |
| 85 | +print(sol.isPalindrome(s)) |
| 86 | +# Output: True |
0 commit comments