90% Python Easy to Follow 2 Pointer

  • 0
        def isPalindrome(self, s):
            s.replace(" ", "")
            s = "".join([char for char in s if char.isalnum()])
            s = s.lower()
            pointer_front = 0
            pointer_back = len(s) - 1
            while pointer_front < pointer_back:
                if s[pointer_front] != s[pointer_back]:
                    return False
                pointer_front += 1
                pointer_back -= 1
            return True

    Pretty simple to follow, just strip String s of spaces and non-alphanumeric characters. Then make it all lowercase.

    After that, have a pointer at the start and have a pointer at the end, and if they are ever not the same you know you don't have a palindrome.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.