> ## 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.

# Rotated Digits Python Solution with Tests

> Tested Python solution for LeetCode 788 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 788, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/rotated-digits/description/).

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

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

## Problem

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

A number is valid if each digit remains a digit after rotation. For example:

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

Given an integer n, return the number of good integers in the range \[1, n].

### Examples

```
Input: n = 10
Output: 4
Explanation: 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.
```

```
Input: n = 1
Output: 0
```

```
Input: n = 2
Output: 1
```

### Constraints

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

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n)
    # Space: O(log n)
    def rotated_digits(self, n: int) -> int:
        valid = frozenset("0125689")
        flipping = frozenset("2569")
        digits = str(n)
        total = 0
        has_flip = False
        for i, ch in enumerate(digits):
            rest = len(digits) - i - 1
            for c in "0123456789"[: int(ch)]:
                if c not in valid:
                    continue
                if has_flip or c in flipping:
                    total += 7**rest
                else:
                    total += 7**rest - 3**rest
            if ch not in valid:
                return total
            has_flip = has_flip or ch in flipping
        return total + (1 if has_flip else 0)
```

## Complexity

| Time | Space |
| - | - |
| O(log n) | O(log n) |

## Tags


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