Advertisement
Shaiai

Valid Palindrome 2 ways.

May 18th, 2022
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. #This is the cheesy way i came up with.
  2. class Solution:
  3.     def isPalindrome(self, s: str) -> bool:
  4.        
  5.         alphnm = ''.join(ch.lower() for ch in s if ch.isalnum())  
  6.         return alphnm == alphnm[::-1]
  7.  
  8. #This is the two pointer solution.
  9. class Solution:
  10.     def isPalindrome(self, s: str) -> bool:
  11.    
  12.         alphnm = ''.join(ch.lower() for ch in s if ch.isalnum())  
  13.         left = 0
  14.         right = len(alphnm) - 1  
  15.        
  16.         while left < right:
  17.             if alphnm[left] != alphnm[right]:
  18.                 return False
  19.             left += 1
  20.             right -= 1
  21.        
  22.         return True
  23.        
  24.  
  25.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement