➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Valid Palindrome

image

  1. We will be given a string and we’re asked to return True if the string is palindrome after passing a conditon else False.
  2. The condition is that the uppercase letters should be converted to lowercase and all non-alphanumeric characters are removed.
  3. For it to be palindrome means that when it reads the same forward and backward.
  4. To solve this I’ll first need to take care of the conditions before checking if it’s palindrome.
  5. To check if it’s palindrome I will implement linear search sort of approach to compare each character from the left to the right array[left] != array[right]

Solve Script: link image

Ehhhh it isn’t optimized

After submitting the solution and looking at other people solution I saw an interesting approach:

Here’s the approach:

The person made use of isalnum() which I didn’t know existed (this would have made my condition check a bit faster!) also instead of using like linear search approach he made use of Not ~ operator

Operator ~ is the bitwise NOT operator (~x == -x-1 => ~0 == -1 => ~1 == -2 and etc), which expects just one argument. It performs logical negation on a given number by flipping all of its bits:

Here’s the script

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = [c.lower() for c in s if c.isalnum()]
        return all (s[i] == s[~i] for i in range(len(s)//2))

Running it works image

Compared to mine in terms of space complexity, mine is better but in terms of time complexity that one is better