nathanwailes

LeetCode 125 - Valid Palindrome - NeetCode solution

Oct 6th, 2023
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. class Solution:
  2.     def isPalindrome(self, s: str) -> bool:
  3.         l, r = 0, len(s) - 1
  4.        
  5.         while l < r:
  6.             while l < r and not self.alphaNum(s[l]):
  7.                 l += 1
  8.             while r > l and not self.alphaNum(s[r]):
  9.                 r -= 1
  10.             if s[l].lower() != s[r].lower():
  11.                 return False
  12.             l, r = l + 1, r - 1
  13.         return True
  14.    
  15.     def alphaNum(self, c):
  16.         return (ord('A') <= ord(c) <= ord('Z') or
  17.                 ord('a') <= ord(c) <= ord('z') or
  18.                 ord('0') <= ord(c) <= ord('9'))
Advertisement
Add Comment
Please, Sign In to add comment