Confusing Number II Problem
Description
LeetCode Problem 1088.
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, and9are rotated180degrees, they become0,1,9,8, and6respectively. - When
2,3,4,5, and7are rotated180degrees, they become invalid.
Note that after rotating a number, we can ignore leading zeros.
- For example, after rotating
8000, we have0008which is considered as just8.
Given an integer n, return the number of confusing numbers in the inclusive range [1, n].
Example 1:
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.
Example 2:
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
Approach: Count Rotatable Numbers, Subtract the Ones That Stay the Same
A number in [1, n] is confusing when (1) every digit is one of 0, 1, 6, 8, 9, so it can be rotated, and (2) the rotated number differs from it. Enumerating every number of type (1) works but there are about two million below 10^9, which is slow. It is much faster to count (1) directly and subtract the few numbers that rotate into themselves.
Count the rotatable numbers in [1, n]. Let n have L digits.
- A shorter number with
kdigits has 4 choices for its first digit (1, 6, 8, 9, not0) and 5 for each other one:4 · 5^(k-1)numbers. - For numbers with
Ldigits, walk n's digits from the left. At positioni, every valid digit smaller than n's digit (not0at the first position) can be followed by anything: addsmaller · 5^(L-1-i). Then continue only if n's own digit is valid, since otherwise no number that matches n so far can stay valid. If all of n's digits are valid, count n itself.
Subtract the numbers that rotate into themselves. Rotating reverses the order of the digits and maps 0→0, 1→1, 6→9, 8→8, 9→6. A number rotates into itself when the digit at position i and the one at position L-1-i form one of the pairs (0,0), (1,1), (6,9), (8,8), (9,6), and a middle digit (odd length) maps to itself: 0, 1 or 8. Build all of them from the outside in, with no leading 0, and count those <= n. There are only a few thousand of them up to ten digits.
A trailing zero is the tricky case: 10 rotates to 01, which is 1, so it is confusing. This works out because such a number can never be one that rotates into itself (its first digit would have to be 0).
For n = 20: the rotatable numbers are 1, 6, 8, 9, 10, 11, 16, 18, 19 (9 of them), the self-rotating ones are 1, 8, 11, and 9 - 3 = 6.
Complexity: O(5L/2) time, where L is the number of digits of n, for listing the self-rotating numbers. O(L) space.
class Solution:
def confusingNumberII(self, n: int) -> int:
s = str(n)
size = len(s)
valid = [0, 1, 6, 8, 9]
# 1. Count x in [1, n] whose digits all rotate (only 0, 1, 6, 8, 9).
total = 0
for k in range(1, size): # shorter than n: first digit 1/6/8/9, then any of the 5
total += 4 * 5 ** (k - 1)
for i, ch in enumerate(s): # same length as n: walk n's digits from the left
d = int(ch)
smaller = sum(1 for v in valid if v < d and (i > 0 or v > 0))
total += smaller * 5 ** (size - 1 - i)
if d not in valid:
break
else:
total += 1 # n itself only uses valid digits
# 2. Subtract the ones that rotate into themselves; they are few, so list them.
pairs = [("0", "0"), ("1", "1"), ("6", "9"), ("8", "8"), ("9", "6")]
same = 0
def build(chars: List[str], lo: int, hi: int) -> None:
nonlocal same
if lo > hi:
if int("".join(chars)) <= n:
same += 1
return
for a, b in pairs:
if lo == 0 and a == "0": # no leading zero (and 0 is not in the range)
continue
if lo == hi and a != b: # the middle digit must rotate into itself
continue
chars[lo], chars[hi] = a, b
build(chars, lo + 1, hi - 1)
for k in range(1, size + 1):
build([""] * k, 0, k - 1)
return total - same