Valid Palindrome

Problem: Valid Palindrome

In this problem, we can use two pointers to check characters, one running from beginning to end, the other one running from end to beginning. When either pointer is not pointing a letter, we move the pointer step forward or backward. Once we found two referenced characters are not the same, we immediately return false.

Code in Python:

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        letters = string.ascii_letters + string.digits
        i, j = 0, len(s)-1
        while i < j:
            if s[i] not in letters:
                i += 1
                continue
            if s[j] not in letters:
                j -= 1
                continue
            if s[i].lower() != s[j].lower():
                return False
            i += 1
            j -= 1

        return True

results matching ""

    No results matching ""