Created
July 2, 2022 22:12
-
-
Save wanderindev/4c6569a7f4d4b0b929d60bf26f262143 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def is_alpha_num(self, c): | |
| return ( | |
| ord("A") <= ord(c) <= ord("Z") | |
| or ord("a") <= ord(c) <= ord("z") | |
| or ord("0") <= ord(c) <= ord("9") | |
| ) | |
| def is_palindrome(self, s): | |
| """ | |
| :type s: str | |
| :rtype: bool | |
| """ | |
| left_pointer = 0 | |
| right_pointer = len(s) - 1 | |
| while left_pointer < right_pointer: | |
| while left_pointer < right_pointer and not self.is_alpha_num( | |
| s[left_pointer] | |
| ): | |
| left_pointer += 1 | |
| while left_pointer < right_pointer and not self.is_alpha_num( | |
| s[right_pointer] | |
| ): | |
| right_pointer -= 1 | |
| if not s[left_pointer].lower() == s[right_pointer].lower(): | |
| return False | |
| left_pointer += 1 | |
| right_pointer -= 1 | |
| return True | |
| # Test cases | |
| sol = Solution() | |
| s = "A man, a plan, a canal: Panama" | |
| assert sol.is_palindrome(s) | |
| s = "race a car" | |
| assert not sol.is_palindrome(s) | |
| s = " " | |
| assert sol.is_palindrome(s) | |
| s = "OP" | |
| assert not sol.is_palindrome(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment