Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- So, if we start traversing inwards, from both ends of the input string, we can expect to see the same characters, in the same order.
- The resulting algorithm is simple:
- - Set two pointers, one at each end of the input string-
- - If the input is palindromic, both the pointers should point to equivalent characters, at all times. 1
- - If this condition is not met at any point of time, we break and return early.
- - We can simply ignore non-alphanumeric characters by continuing to traverse further.
- - Continue traversing inwards until the pointers meet in the middle.
- """
- class Solution:
- def isPalindrome(self, s: str) -> bool:
- def is_alphabet(c):
- return ord(c) - ord("a") < 26 or ord(c) - ord("A") < 26
- if len(s) == 1:
- return is_alphabet(s[0])
- if len(s) == 0:
- return False
- start = 0
- end = len(s) - 1
- while (start<end):
- if not is_alphabet(s[start]):
- start = start+1
- continue
- if not is_alphabet(s[end]):
- end = end - 1
- continue
- if s[start].lower() == s[end].lower():
- start = start + 1
- end = end - 1
- continue
- else:
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement