PYTHON 29ms 97% solution


  • 0
    V
    class Solution(object):
        def isStrobogrammatic(self, num):
            """
            :type num: str
            :rtype: bool
            """
            strobopairs = {
                '6': '9',
                '9': '6',
                '8': '8',
                '1': '1',
                '0': '0'
            }
            
            if len(num) == 1:
                if num == '6' or num == '9':
                    return False
                return num in strobopairs
            
            left = 0
            right = len(num) - 1
            
            while left < right:
                lchar = num[left]
                if not (lchar in strobopairs and strobopairs[lchar] == num[right]):
                    return False
                left += 1
                right -= 1
            
            if left == right:
                if num[left] == '6' or num[left] == '9':
                    return False
                return num[left] in strobopairs
            
            return True
    

Log in to reply
 

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