Valid Palindrome String | LeetCode | InterviewBit | Solution

LeetCode Problem Link. InterviewBit Problem Link.

class Solution:
    def isPalindrome(self, s):
        ns = ""
        for c in s.lower():
            c = c
            if 'a' <= c <= 'z' or '0' <= c <= '9':
                ns += c
        return int(ns == ns[::-1])
class Solution:
    def isPalindrome(self, s: str) -> bool:
        l, r = 0, len(s)-1
        s = s.lower()
        while l < r:
            if s[l] == s[r]:
                l += 1
                r -= 1
            elif not ('a' <= s[l] <= 'z' or '0' <= s[l] <= '9'):
                l += 1
            elif not ('a' <= s[r] <= 'z' or '0' <= s[r] <= '9'):
                r -= 1
            else:
                return False
        return True

    Leave a Reply

    Your email address will not be published. Required fields are marked *