Skip to content

125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/

js
/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
	s = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
	return s.split('').reverse().join('') === s;
};
py
class Solution(object):

    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        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