Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1088: Confusing Number II

In this guide, we solve Leetcode #1088 Confusing Number II in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Math, Backtracking

Intuition

We must explore combinations of choices, but many branches can be pruned early.

Backtracking enumerates valid candidates while keeping the search space under control.

Approach

Use DFS to build candidates step by step, and backtrack when constraints are violated.

Pruning keeps the exploration practical for typical constraints.

Steps:

  • Define the decision tree.
  • DFS through choices and backtrack.
  • Prune invalid paths early.

Example

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.

Python Solution

class Solution: def confusingNumberII(self, n: int) -> int: def check(x: int) -> bool: y, t = 0, x while t: t, v = divmod(t, 10) y = y * 10 + d[v] return x != y def dfs(pos: int, limit: bool, x: int) -> int: if pos >= len(s): return int(check(x)) up = int(s[pos]) if limit else 9 ans = 0 for i in range(up + 1): if d[i] != -1: ans += dfs(pos + 1, limit and i == up, x * 10 + i) return ans d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6] s = str(n) return dfs(0, True, 0)

Complexity

The time complexity is Exponential (worst case). The space complexity is O(depth).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy