Skip to content
LC-0125 Easy LeetCode

125. Valid Palindrome

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 51% Topics: Two Pointers, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    # @param s, a string
    # @return a boolean
    def isPalindrome(self, s):
        i, j = 0, len(s) - 1
        while i < j:
            while i < j and not s[i].isalnum():
                i += 1
            while i < j and not s[j].isalnum():
                j -= 1
            if s[i].lower() != s[j].lower():
                return False
            i, j = i + 1, j - 1
        return True

Solution from kamyu104/LeetCode-Solutions · MIT