Problem statement

https://leetcode.com/problems/confusing-number/

Solution

Create hash table of connections and then check definition: apply d for every digit and reverse it. Also deal with case when we have # symbol.

Complexity

Time and space complexity is O(m), where m is nubmer of digits in n.

Code

class Solution:
    def confusingNumber(self, n):
        s = str(n)
        d = {"0" : "0", "1" : "1", "6" : "9", "8" : "8", "9" : "6"}
        s1 = "".join(d.get(c,"#") for c in s[::-1])     
        return s1 != s and "#" not in s1