<p>An integer <code>x</code> is a <strong>good</strong> if after rotating each digit individually by 180 degrees, we get a valid number that is different from <code>x</code>. Each digit must be rotated - we cannot choose to leave it alone.</p>

<p>A number is valid if each digit remains a digit after rotation. For example:</p>

<ul>
	<li><code>0</code>, <code>1</code>, and <code>8</code> rotate to themselves,</li>
	<li><code>2</code> and <code>5</code> rotate to each other (in this case they are rotated in a different direction, in other words, <code>2</code> or <code>5</code> gets mirrored),</li>
	<li><code>6</code> and <code>9</code> rotate to each other, and</li>
	<li>the rest of the numbers do not rotate to any other number and become invalid.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>the number of <strong>good</strong> integers in the range </em><code>[1, n]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 0
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:


    def rotatedDigits(self, n):
        # all digits should one of [2, 5, 6, 9] + [0, 1, 8]
        ans = 0
        digits = set() # digits we have consumed so far
        # if the digits we have are only 0,1,8, these are insufficient alone
        insufficient = set([0,1,8])
        valid = set([2, 5, 6, 9, 0, 1, 8])
        

        str_n = str(n)
        for i,c in enumerate(str_n):
            for j in range(int(c)):
                if digits.issubset(valid) and j in valid:
                    # generate permutations on str_n[i + 1:]
                    ans += max(1, 7 ** (len(str_n) - i - 1))
                if digits.issubset(insufficient) and j in insufficient:
                    # remove permutations where generated number only has 0,1,8
                    ans -= max(1, 3 ** (len(str_n) - i - 1))
            digits.add(int(c))
            if int(c) not in valid:
                # no answers after this point
                break

        # last number is not checked
        last_number = set([int(c) for c in str_n])
        if digits.issubset(valid) and not digits.issubset(insufficient):
            ans += 1
        
        return ans
                




        

    # intuitive O(nlogn) approach
    def rotatedDigits1(self, n: int) -> int:
        # 23 invalid
        # 11 invalid (same number)
        # 121 valid
        # 1111112 valid
        # 76 invalid
        # 0

        # if we see 0 1 8, skip character
        # in the string, we must see at least one of [2,5,6,9]
        # if we see any other character in the string --> invalid

        ans = 0
        for i in range(1, ):
            required_num = False
            while i > 0:
                digit = i % 10
                i = i // 10
                if digit in [0, 1, 8]:
                    continue
                elif digit in [2, 5, 6, 9]:
                    required_num = True
                else:
                    required_num = False
                    break
            if required_num:
                ans += 1
        return ans


16700