Problem
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Find all strobogrammatic numbers that are of length = n.
Examples
Example 1:
|
|
Similar Problems
Strobogrammatic Number 1 - Check if strobogrammatic
Solution
Method 1 - DFS
We can use following approach:
- Use recursion to generate strobogrammatic numbers by placing strobogrammatic pairs at the start and end of the number.
- Handle the base cases where
n
is 0 or 1.n == 0
⇨ empty stringn == 1
⇨[0,1,8]
- Construct numbers of length
n
by placing valid strobogrammatic pairs and recursively constructing the inner part of the number.
|
|
Code
|
|
Complexity
- ⏰ Time complexity:
O(5^(n/2))
, each time we generate 5 pairs - 🧺 Space complexity:
O(n)
, for using recursion stack