[
math
hash table
]
Leetcode 0246. Strobogrammatic Number
Problem statement
https://leetcode.com/problems/strobogrammatic-number/
Solution
Create hash table of corresponing digits and then check all pairs between original and reversed number.
Complexity
Time complexity is $O(m)$, where $m$ is length of num
, space complexity is $O(10)$.
Code
class Solution:
def isStrobogrammatic(self, num):
d = {"0":"0", "1":"1", "6":"9", "9":"6", "8":"8", "2":"", "3":"", "4":"", "5":"", "7":""}
return all(i==d[j] for i, j in zip(num, num[::-1]))