Skip to main content

Strobogrammatic Number II - Solution & Explanation

MediumPremiumFree on FleetCodeArrayStringRecursion7 min readAsked at: Meta, Google
Practice this problem

Problem Statement

Given an integer n, return all the strobogrammatic numbers that are of length n. You may return the answer in any order.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

 

Example 1:

Input: n = 2
Output: ["11","69","88","96"]

Example 2:

Input: n = 1
Output: ["0","1","8"]

 

Constraints:

  • 1 <= n <= 14

Approach Overview

Problem Overview: Generate all strobogrammatic numbers of length n. A number is strobogrammatic if it looks the same when rotated 180 degrees. Valid digit pairs are (0,0), (1,1), (6,9), (8,8), and (9,6). The result must include every valid number of length n without leading zeros (except when n = 1).

Approach 1: Brute Force Generation + Validation (O(10^n * n) time, O(n) space)

The most direct idea is to generate every possible n-digit number and check whether it remains valid after a 180-degree rotation. For each candidate, iterate through the digits from both ends and verify the rotation mapping: 0→0, 1→1, 6→9, 8→8, 9→6. Any digit outside this set immediately invalidates the number. This approach performs up to 10^n checks and each validation takes O(n) time, making it extremely slow even for moderate values of n. It demonstrates the property of strobogrammatic numbers but is not practical for interviews.

Approach 2: Recursive Pair Construction (O(5^(n/2)) time, O(5^(n/2)) space)

The efficient approach builds numbers from the center outward using valid strobogrammatic digit pairs. Instead of generating every number, you only place digits that remain valid after rotation. Use recursion to construct smaller strobogrammatic strings of length n-2, then wrap them with valid pairs like "1" + middle + "1" or "6" + middle + "9". The base cases are length 0 (return an empty string) and length 1 (return 0, 1, 8). Avoid placing 0 at the outermost level to prevent leading zeros.

Each recursion level adds symmetric pairs around the inner string. Since there are at most five valid pairs and the recursion depth is roughly n/2, the total number of generated combinations is about 5^(n/2). This directly generates only valid numbers, which is far more efficient than filtering invalid candidates.

The algorithm works naturally with recursion because each valid number can be decomposed into an inner strobogrammatic string plus a symmetric outer pair. The result set is stored using simple string construction, and iteration over candidate pairs is straightforward.

Recommended for interviews: Recursive pair construction is the expected solution. It shows that you recognized the symmetry property of strobogrammatic numbers and avoided brute force enumeration. Mentioning the brute force idea first shows understanding of the problem space, but implementing the recursive construction demonstrates stronger algorithmic thinking.

Solution

If the length is 1, then the strobogrammatic numbers are only 0, 1, 8; if the length is 2, then the strobogrammatic numbers are only 11, 69, 88, 96.

We design a recursive function dfs(u), which returns the strobogrammatic numbers of length u. The answer is dfs(n).

If u is 0, return a list containing an empty string, i.e., [""]; if u is 1, return the list ["0", "1", "8"].

If u is greater than 1, we traverse all the strobogrammatic numbers of length u - 2. For each strobogrammatic number v, we add 1, 8, 6, 9 to both sides of it, and we can get the strobogrammatic numbers of length u.

Note that if u neq n, we can also add 0 to both sides of the strobogrammatic number.

Finally, we return all the strobogrammatic numbers of length n.

The time complexity is O(2^{n+2}).

Similar problems:

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Generation + ValidationO(10^n * n)O(n)Conceptual baseline to understand strobogrammatic validation
Recursive Pair ConstructionO(5^(n/2))O(5^(n/2))Generating all valid numbers efficiently using symmetry

Video Solution

LeetCode 247. Strobogrammatic Number II Explanation and Solution • happygirlzt • 8,500 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Strobogrammatic Number II easy or hard?
Strobogrammatic Number II is generally rated Medium difficulty. The challenge is recognizing that the number must be constructed symmetrically using valid rotation pairs rather than brute forcing all numbers.
Strobogrammatic Number II Python/Java solution
Python, Java, C++, and Go implementations typically follow the same recursive pattern. A helper function generates strobogrammatic strings of length n-2 and wraps them with valid pairs such as (1,1), (6,9), and (9,6) while avoiding leading zeros.
How to solve Strobogrammatic Number II in O(n)?
Generating all strobogrammatic numbers cannot be done in O(n) time because the number of valid outputs grows exponentially. The best practical solution constructs numbers recursively using valid rotation pairs, producing around 5^(n/2) combinations.
What is the best approach for Strobogrammatic Number II?
Recursive pair construction is the most efficient approach. Instead of checking every number, the algorithm builds valid strobogrammatic numbers by placing symmetric digit pairs like (1,1), (6,9), and (9,6) around smaller valid strings. This reduces the search space and runs in about O(5^(n/2)) time.
Is Strobogrammatic Number II asked at Google/Amazon/Meta?
Strobogrammatic problems have appeared in interviews at companies like Google, Amazon, and Meta, especially for roles that emphasize recursion and string manipulation. Variants such as Strobogrammatic Number I and III are also common.
What data structure is used in Strobogrammatic Number II?
The solution primarily uses recursion and string construction. A list or array stores intermediate results, while recursion builds numbers from the center outward using valid digit pairs.
What is the time complexity of Strobogrammatic Number II?
The optimal recursive solution runs in O(5^(n/2)) time because each level of recursion can add up to five valid digit pairs and the recursion depth is roughly n/2. Space complexity is also O(5^(n/2)) since all valid numbers of length n must be stored in the result list.

Ready to solve this problem?

Practice Strobogrammatic Number II with our built-in code editor and test cases.

Practice on FleetCode