For the given length n, find all n-length Strobogrammatic numbers.
Strobogrammatic Number is a number whose numeral is rotationally symmetric so that it appears the same when rotated 180 degrees. In other words, Strobogrammatic Number appears the same right-side up and upside down.
0 after 180° rotation : (0 → 0)
1 after 180° rotation : (1 → 1)
8 after 180° rotation : (8 → 8)
6 after 180° rotation : (6 → 9)
9 after 180° rotation : (9 → 6)
Examples :
Input : n = 2
Output : 88 11 96 69
Input : n = 4
Output : 8008 1001 9006 6009 8888 1881 9886 6889 8118 1111
9116 6119 8968 1961 9966 6969 8698 1691 9696 6699
Code:
Below is the Python3 implementation :
C++
#include <bits/stdc++.h>
using namespace std;
vector<string> numdef( int n, int length)
{
vector<string> result;
if (n == 0){
result.push_back( "" );
return result;
}
if (n == 1) {
result.push_back( "1" );
result.push_back( "0" );
result.push_back( "8" );
return result;
}
vector<string> middles = numdef(n - 2, length);
for (string middle : middles) {
if (n != length)
result.push_back( "0" + middle + "0" );
result.push_back( "8" + middle + "8" );
result.push_back( "1" + middle + "1" );
result.push_back( "9" + middle + "6" );
result.push_back( "6" + middle + "9" );
}
return result;
}
vector<string> strobogrammatic_num( int n)
{
vector<string> result = numdef(n, n);
return result;
}
int main()
{
for (string num : (strobogrammatic_num(2)))
cout << num << " " ;
}
|
Java
import java.util.*;
class GFG {
static ArrayList<String> numdef( int n, int length)
{
ArrayList<String> result = new ArrayList<String>();
if (n == 0 ){
result.add( "" );
return result;
}
if (n == 1 ) {
result.add( "1" );
result.add( "0" );
result.add( "8" );
return result;
}
ArrayList<String> middles = numdef(n - 2 , length);
for (String middle : middles) {
if (n != length)
result.add( "0" + middle + "0" );
result.add( "8" + middle + "8" );
result.add( "1" + middle + "1" );
result.add( "9" + middle + "6" );
result.add( "6" + middle + "9" );
}
return result;
}
static ArrayList<String> strobogrammatic_num( int n)
{
ArrayList<String> result = numdef(n, n);
return result;
}
public static void main(String[] args)
{
for (String num : (strobogrammatic_num( 3 )))
System.out.print(num + " " );
}
}
|
Python3
def strobogrammatic_num(n):
result = numdef(n, n)
return result
def numdef(n, length):
if n = = 0 : return [""]
if n = = 1 : return [ "1" , "0" , "8" ]
middles = numdef(n - 2 , length)
result = []
for middle in middles:
if n ! = length:
result.append( "0" + middle + "0" )
result.append( "8" + middle + "8" )
result.append( "1" + middle + "1" )
result.append( "9" + middle + "6" )
result.append( "6" + middle + "9" )
return result
if __name__ = = '__main__' :
print (strobogrammatic_num( 3 ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< string > numdef( int n, int length)
{
List< string > result = new List< string >();
if (n == 0)
return result;
if (n == 1) {
result.Add( "1" );
result.Add( "0" );
result.Add( "8" );
return result;
}
List< string > middles = numdef(n - 2, length);
foreach ( string middle in middles)
{
if (n != length)
result.Add( "0" + middle + "0" );
result.Add( "8" + middle + "8" );
result.Add( "1" + middle + "1" );
result.Add( "9" + middle + "6" );
result.Add( "6" + middle + "9" );
}
return result;
}
static List< string > strobogrammatic_num( int n)
{
List< string > result = numdef(n, n);
return result;
}
public static void Main( string [] args)
{
foreach ( string num in (strobogrammatic_num(3)))
Console.Write(num + " " );
}
}
|
Javascript
function strobogrammatic_num(n)
{
let result = numdef(n, n)
return result
}
function numdef(n, length)
{
if (n == 0) return [ "" ]
if (n == 1) return [ "1" , "0" , "8" ]
let middles = numdef(n - 2, length)
let result = []
for ( var middle of middles)
{
if (n != length)
result.push( "0" + middle + "0" )
result.push( "8" + middle + "8" )
result.push( "1" + middle + "1" )
result.push( "9" + middle + "6" )
result.push( "6" + middle + "9" )
}
return result
}
console.log(strobogrammatic_num(3))
|
Time Complexity: O(5^n)
Auxiliary Space: O(5^n)
Reference : https://en.wikipedia.org/wiki/Strobogrammatic_number
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...