Ruby O(n) time O(1) space solution with two pointers


  • 0
    S
    def is_palindrome(s)
        i, j = 0, s.length - 1
        while i < j
            i += 1 while !(s[i] =~ /[A-Za-z0-9]/) && i < s.length - 1
            j -= 1 while !(s[j] =~ /[A-Za-z0-9]/) && j > 0
            return true if i == s.length - 1 && j == 0
            return false if s[i].downcase != s[j].downcase
            i += 1
            j -= 1
        end
        true
    end
    

Log in to reply
 

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