Advertisement
smj007

Valid Palindrome

Apr 18th, 2025 (edited)
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. class Solution:
  2.     def isPalindrome(self, s: str) -> bool:
  3.  
  4.         def is_alphanumeric(c: str) -> bool:
  5.             if  ord("A") <= ord(c) <= ord("Z"):
  6.                 return True
  7.             if ord("a") <= ord(c) <= ord("z"):
  8.                 return True
  9.             if ord("0") <= ord(c) <= ord("9"):
  10.                 return True
  11.  
  12.             return False
  13.  
  14.         left = 0
  15.         right = len(s) - 1
  16.  
  17.         while(left<right):
  18.             if s[left] == " ":
  19.                 left += 1
  20.                 continue
  21.  
  22.             if s[right] == " ":
  23.                 right -= 1
  24.                 continue
  25.  
  26.             if not is_alphanumeric(s[left]):
  27.                 left += 1
  28.                 continue
  29.  
  30.             if not is_alphanumeric(s[right]):
  31.                 right -= 1
  32.                 continue
  33.  
  34.             if s[left].lower() == s[right].lower():
  35.                 left += 1
  36.                 right -= 1
  37.             else:
  38.                 return False
  39.  
  40.         return True
  41.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement