Smallest number containing all possible N length permutations using digits 0 to D
Last Updated :
03 May, 2023
Given two integer N and D, the task is to find the size of the smallest string that contains all permutations of length N that can be formed using first D digits (0, 1, …, D-1).
Examples:
Input: N = 2, D = 2
Output: 01100
Explanation:
Possible permutations of length 2 from digits (0, 1) are {00, 01, 10, 11}.
“01100” is one such string that contains all the permutations as a substring.
Other possible answers are “00110”, “10011”, “11001”
Input: N = 2, D = 4
Output: 03322312113020100
Explanation:
Here all possible permutations of length 2 from digits {0, 1, 2, 3} are
00 10 20 30
01 11 21 31
02 12 22 32
03 13 23 33
“03322312113020100” is a string of minimum length that contains all the above permutations.
Approach:
Append ‘0’ N-1 times and call DFS on the string in the current state. Append all the D characters one by one. Every time after appending, check if the new string is visited or not. If so, mark it visited by inserting it into a HashSet and add this character in the answer. Recursively call the DFS function on the last D characters. Repeat this process until all possible substrings of N length from the D digits are appended to the string. Print the final string generated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
set<string> visited;
string ans;
void dfs(string curr, int D)
{
for ( int x = 0; x < D; ++x) {
char chr = x + '0' ;
string neighbour = curr + chr;
if (visited.find(neighbour) == visited.end()) {
visited.insert(neighbour);
dfs(neighbour.substr(1), D);
ans.push_back(chr);
}
}
}
string reqString( int N, int D)
{
if (N == 1 && D == 1)
return "0" ;
visited.clear();
ans.clear();
string start;
for ( int i = 0; i < N - 1; i++)
start.append( "0" );
dfs(start, D);
ans.append(start);
return ans;
}
int main()
{
int N = 2;
int D = 2;
cout << reqString(N, D) << '\n' ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GeeksforGeeks {
static Set<String> visited;
static StringBuilder ans;
public static String reqString( int N,
int D)
{
if (N == 1 && D == 1 )
return "0" ;
visited = new HashSet<>();
ans = new StringBuilder();
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < N - 1 ; ++i) {
sb.append( "0" );
}
String start = sb.toString();
dfs(start, D);
ans.append(start);
return new String(ans);
}
public static void dfs(String curr, int D)
{
for ( int x = 0 ; x < D; ++x) {
String neighbour = curr + x;
if (!visited.contains(neighbour)) {
visited.add(neighbour);
dfs(neighbour.substring( 1 ), D);
ans.append(x);
}
}
}
public static void main(String args[])
{
int N = 2 ;
int D = 2 ;
System.out.println(reqString(N, D));
}
}
|
Python3
visited = set ()
ans = []
def dfs(curr, D):
for c in range (D):
c = str (c)
neighbour = curr + c
if neighbour not in visited:
visited.add(neighbour)
dfs(neighbour[ 1 :], D)
ans.append(c)
def reqString(N, D):
if (N = = 1 and D = = 1 ):
return "0"
start = ' '.join([' 0 '] * (N - 1 ))
dfs(start, D)
ans.extend([ '0' ] * (N - 1 ))
return ''.join(ans)
if __name__ = = '__main__' :
N, D = 2 , 2
print (reqString(N,D))
|
C#
using System;
using System.Collections.Generic;
using System.Text;
public class GFG {
static HashSet< string > visited;
static StringBuilder ans;
static string reqString( int N, int D)
{
if (N == 1 && D == 1)
return "0" ;
visited = new HashSet< string >();
ans = new StringBuilder();
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < N - 1; ++i) {
sb.Append( "0" );
}
string start = sb.ToString();
dfs(start, D);
ans.Append(start);
return ans.ToString();
}
static void dfs( string curr, int D)
{
for ( int x = 0; x < D; ++x)
{
string neighbour = curr + x;
if (!visited.Contains(neighbour))
{
visited.Add(neighbour);
dfs(neighbour.Substring(1), D);
ans.Append(x.ToString());
}
}
}
static public void Main()
{
int N = 2;
int D = 2;
Console.WriteLine(reqString(N, D));
}
}
|
Javascript
<script>
let visited = new Set()
let ans=[]
function dfs(curr, D){
for (let c = 0;c<D;c++){
c = parseInt(c)
let neighbour = curr + c
if (visited.has(neighbour) == false ){
visited.add(neighbour)
dfs(neighbour.substring(1), D)
ans.push(c)
}
}
}
function reqString(N, D){
if (N == 1 && D == 1)
return "0"
let start= new Array(N - 1).fill( '0' ).join( '' )
dfs(start, D)
ans.push( new Array(N - 1).fill( '0' ))
return ans.join( '' )
}
let N = 2,D = 2
document.write(reqString(N,D))
</script>
|
Time Complexity: O(N * DN)
Auxiliary Space: O(N * DN)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...