Strobogrammatic Number

Problem: Strobogrammatic Number

It's clear that corresponding number couples are quite fixed. We can store strobogrammatic-making numbers in a hash table and check whether couples from the number are in the table.

We can use two pointers, one going from first to last and the other going reversely to check number pairs. Besides, we also need to check the digit in the middle since if it's a number like 2, the whole number won't be strobogrammatic.

Code in Python:

class Solution(object):
    def isStrobogrammatic(self, num):
        """
        :type num: str
        :rtype: bool
        """
        pairs = [(6, 9), (9, 6), (0, 0), (1, 1), (8, 8)]
        l, r = 0, len(num)-1
        while l <= r:
            if (int(num[l]), int(num[r])) in pairs:
                l, r = l + 1, r - 1
            else:
                return False
        return True

results matching ""

    No results matching ""