Strobogrammatic Number II - Problem

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

The valid strobogrammatic digits are: 0, 1, 6, 8, 9 where:

  • 0 rotates to 0
  • 1 rotates to 1
  • 6 rotates to 9
  • 8 rotates to 8
  • 9 rotates to 6

Input & Output

Example 1 — Small Case
$ Input: n = 2
Output: ["11","69","88","96"]
💡 Note: All 2-digit strobogrammatic numbers: 11 (1↔1), 69 (6↔9), 88 (8↔8), 96 (9↔6)
Example 2 — Single Digit
$ Input: n = 1
Output: ["0","1","8"]
💡 Note: Single digits that look the same upside down: 0, 1, and 8
Example 3 — Larger Case
$ Input: n = 4
Output: ["1001","1111","1691","1881","1961","6009","6119","6699","6889","6969","8008","8118","8698","8888","8968","9006","9116","9696","9886","9966"]
💡 Note: 4-digit numbers built by adding valid pairs around 2-digit strobogrammatic numbers, excluding those with leading zeros

Constraints

  • 0 ≤ n ≤ 14

Visualization

Tap to expand
Strobogrammatic Number II: Generate All n-Length NumbersInputn = 2Valid Strobogrammatic Pairs0↔0, 1↔1, 6↔9, 8↔8, 9↔6Build 2-digit: 11, 69, 88, 96Output["11","69","88","96"]111 ↔ 1696 ↔ 9888 ↔ 8969 ↔ 6✓ All numbers look the same when rotated 180°
Understanding the Visualization
1
Input
Length n = 2
2
Valid Digits
Use pairs: 0↔0, 1↔1, 6↔9, 8↔8, 9↔6
3
Output
All valid 2-digit strobogrammatic numbers
Key Takeaway
🎯 Key Insight: Build strobogrammatic numbers recursively by placing valid digit pairs symmetrically from outside to inside
Asked in
Google 30 Facebook 25 Amazon 20
28.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen