-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125_Valid_Palindrome.py
More file actions
55 lines (45 loc) · 1.98 KB
/
125_Valid_Palindrome.py
File metadata and controls
55 lines (45 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
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.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
"""
class Solution:
def isPalindrome(self, s: str) -> bool:
# Initialize two pointers: left (l) at the beginning, right (r) at the end of the string
l , r = 0 , len(s)-1
# Loop until the two pointers meet
# Alphanumeric means a character is either (A-Z or a-z) or (0-9)
while l<r :
#Move left pointer forward if current character is not alphanumeric
while l<r and not self.alphaNum(s[l]):
l+=1
#Move right pointer backward if current character is not alphanumeric
while r>l and not self.alphaNum(s[r]):
r-=1
# Compare characters at left and right (case-insensitive)
if s[l].lower() != s[r].lower():
return False # Not a Palindrome
# Move both pointers closer towards the center
l,r = l+1, r-1
return True # It is a Palindrome
# Helper function to check if a character is alphanumeric
def alphaNum(self,c):
# ord returns the ASCII number
return(ord('A') <= ord(c) <= ord('Z') or
ord('a') <= ord(c) <= ord('z') or
ord('0') <= ord(c) <= ord('9'))