nathanwailes

LeetCode 125 - Valid Palindrome - 2023.10.07 solution

Oct 6th, 2023
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. class Solution:
  2.     def isPalindrome(self, s: str) -> bool:
  3.         """ Proposed solution: convert the string to lowercase, then have two pointers
  4.        step inwards from the outside.
  5.        """
  6.         s = s.lower()
  7.  
  8.         # Have them start outside of the range of the word so that I don't need to consider the
  9.         # first iteration as a special case.
  10.         l = -1
  11.         r = len(s)
  12.         while l < r:
  13.             l += 1
  14.             r -= 1
  15.  
  16.             # have them step inwards until they're both on a new character
  17.             # I need to consider
  18.             while l < r and not s[l].isalnum():
  19.                 l += 1
  20.             while r > l and not s[r].isalnum():
  21.                 r -= 1
  22.            
  23.             # compare them
  24.             if s[l] != s[r]:
  25.                 return False
  26.         return True
  27.  
Advertisement
Add Comment
Please, Sign In to add comment