smj007

Untitled

Jun 18th, 2022
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. class Solution:
  2.    
  3.     def check_alphabet(self, s: str):
  4.         if ord(s) >= ord('a') and ord(s) <= ord('z'):
  5.             return True
  6.         if ord(s) >= ord('A') and ord(s) <= ord('Z'):
  7.             return True
  8.         if ord(s) >= ord('0') and ord(s) <= ord('9'):
  9.             return True
  10.         return False
  11.    
  12.     def isPalindrome(self, s: str) -> bool:
  13.        
  14.         # remove special characters
  15.         left = 0
  16.         right = len(s) - 1
  17.        
  18.         while(left < right):
  19.            
  20.             while(left < len(s) and not self.check_alphabet(s[left])):
  21.                 left += 1
  22.                
  23.             while(right >= 0  and not self.check_alphabet(s[right])):
  24.                 right -= 1
  25.                
  26.             if left > right:
  27.                 break
  28.                
  29.             if s[left].lower() != s[right].lower():
  30.                 return False
  31.            
  32.             left += 1
  33.             right += -1
  34.            
  35.         return True
Advertisement
Add Comment
Please, Sign In to add comment