LeetCode 246: Strobogrammatic Number

link

Two ends must form a Strobogrammatic pair and if there is a center it must be Strobogrammatic.

Time: \mathcal{O}(n), space: \mathcal{O}(1).

class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        strobo_pairs = {"00", "11", "69", "88", "96"}
        strobo_centers = {"0", "1", "8"}

        l, r = 0, len(num)-1
        while l < r:
            if num[l]+num[r] not in strobo_pairs:
                return False
            l += 1
            r -= 1

        # check center
        if l == r and num[l] not in strobo_centers:
            return False

        return True

Leave a comment