Skip to content

Instantly share code, notes, and snippets.

@munguial
Created August 6, 2020 05:13
Show Gist options
  • Save munguial/866a69c3cc89ea3f44ccb7fa1fc86122 to your computer and use it in GitHub Desktop.
Save munguial/866a69c3cc89ea3f44ccb7fa1fc86122 to your computer and use it in GitHub Desktop.
August - Day 3 - Valid Palindrome
class Solution:
def isPalindrome(self, s: str) -> bool:
def isValid(c: int) -> bool:
if (c >= ord('0') and c <= ord('9')) or \
(c >= ord('A') and c <= ord('Z')) or \
(c >= ord('a') and c <= ord('z')):
return True
return False
def sameChar(i: int, j: int) -> bool:
if s[i] == s[j] or \
(ord(s[i]) > ord('9') and \
ord(s[j]) > ord('9') and \
abs(ord(s[j]) - ord(s[i])) == 32):
return True
return False
i = 0
j = len(s) - 1
while i < j:
while i < len(s) and not isValid(ord(s[i])):
i += 1
while j >= 0 and not isValid(ord(s[j])):
j -= 1
if i > j:
return True
if not sameChar(i, j):
return False
i += 1
j -= 1
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment