nathanwailes

LeetCode 125 - Valid Palindrome - 2023.04.01 solution

Apr 1st, 2023
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. class Solution:
  2.     def isPalindrome(self, s: str) -> bool:
  3.         """ Have two pointers step inwards, comparing the character at each index.
  4.        """
  5.         l = -1
  6.         r = len(s)
  7.  
  8.         while l < r:
  9.             l += 1
  10.             while l < len(s) and not s[l].lower().isalnum():
  11.                 l += 1
  12.            
  13.             r -= 1
  14.             while r >= 0 and not s[r].lower().isalnum():
  15.                 r -= 1
  16.            
  17.             if l >= len(s) and r >= 0:
  18.                 return False
  19.             if r < 0 and l < len(s):
  20.                 return False
  21.             if l >= len(s) and r < 0:
  22.                 return True
  23.  
  24.             if s[l].lower() != s[r].lower():
  25.                 return False
  26.        
  27.         return True
Advertisement
Add Comment
Please, Sign In to add comment