> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Confusing Number II Python Solution with Tests

> Tested Python solution for LeetCode 1088 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 1088, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [Backtracking](/catalog/topics/backtracking), [Depth-First Search](/catalog/topics/depth-first-search). [View on LeetCode](https://leetcode.com/problems/confusing-number-ii/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1088   # by problem number
lcpy gen -s confusing_number_ii   # by problem name
```

## Problem

A **confusing number** is a number that when rotated `180` degrees becomes a **different** number with each digit valid.

We can rotate digits of a number by `180` degrees to form new digits.

* When `0, 1, 6, 8,` and `9` are rotated `180` degrees, they become `0, 1, 9, 8,` and `6` respectively.
* When `2, 3, 4, 5,` and `7` are rotated `180` degrees, they become **invalid**.

Note that after rotating a number, we can ignore leading zeros.

* For example, after rotating `8000`, we have `0008` which is considered as just `8`.

Given an integer `n`, return *the number of **confusing numbers** in the inclusive range* `[1, n]`.

### Examples

```
Input: n = 20
Output: 6
Explanation: The confusing numbers are [6,9,10,16,18,19].
6 converts to 9.
9 converts to 6.
10 converts to 01 which is just 1.
16 converts to 91.
18 converts to 81.
19 converts to 61.
```

```
Input: n = 100
Output: 19
Explanation: The confusing numbers are [6,9,10,16,18,19,60,61,66,68,80,81,86,89,90,91,98,99,100].
```

### Constraints

* 1 \<= n \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/confusing_number_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/confusing_number_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(5^d) for d digits of n
    # Space: O(d)
    def confusing_number_ii(self, n: int) -> int:
        rotate = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
        digits = str(n)

        def check(x: int) -> bool:
            y, t = 0, x
            while t:
                t, v = divmod(t, 10)
                y = y * 10 + rotate[v]
            return x != y

        def dfs(pos: int, bounded: bool, x: int) -> int:
            if pos == len(digits):
                return int(check(x))
            up = int(digits[pos]) if bounded else 9
            total = 0
            for i in range(up + 1):
                if rotate[i] != -1:
                    total += dfs(pos + 1, bounded and i == up, x * 10 + i)
            return total

        return dfs(0, True, 0)
```

## Complexity

| Time | Space |
| - | - |
| O(5^d) for d digits of n | O(d) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.