Given two integer **N** and **K**, 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

Explaination:

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:

## Java

`// Java Program to find the ` `// minimum length string ` `// consisting of all ` `// permutations of length N ` `// of D digits ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GeeksforGeeks { ` ` ` ` ` `// Initialize hashset to see ` ` ` `// if all the possible ` ` ` `// permutations are present ` ` ` `// in the min length string ` ` ` `static` `Set<String> visited; ` ` ` `// To keep min length string ` ` ` `static` `StringBuilder ans; ` ` ` ` ` `public` `static` `String reqString(` `int` `N, ` ` ` `int` `D) ` ` ` `{ ` ` ` `// Base case ` ` ` `if` `(N == ` `1` `&& D == ` `1` `) ` ` ` `return` `"0"` `; ` ` ` `visited = ` `new` `HashSet<>(); ` ` ` `ans = ` `new` `StringBuilder(); ` ` ` ` ` `StringBuilder sb = ` `new` `StringBuilder(); ` ` ` `// Append '0' n-1 times ` ` ` `for` `(` `int` `i = ` `0` `; i < N - ` `1` `; ++i) { ` ` ` `sb.append(` `"0"` `); ` ` ` `} ` ` ` `String start = sb.toString(); ` ` ` `// Call the DFS Function ` ` ` `dfs(start, D); ` ` ` `ans.append(start); ` ` ` ` ` `return` `new` `String(ans); ` ` ` `} ` ` ` `// Generate the required string ` ` ` `public` `static` `void` `dfs(String curr, ` `int` `D) ` ` ` `{ ` ` ` `// Iterate over all the possible ` ` ` `// character ` ` ` `for` `(` `int` `x = ` `0` `; x < D; ++x) { ` ` ` `// Append to make a new string ` ` ` `String neighbour = curr + x; ` ` ` `// If the new string is not ` ` ` `// visited ` ` ` `if` `(!visited.contains(neighbour)) { ` ` ` `// Add in hashset ` ` ` `visited.add(neighbour); ` ` ` `// Call the dfs function on ` ` ` `// the last d characters ` ` ` `dfs(neighbour.substring(` `1` `), D); ` ` ` ` ` `ans.append(x); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `int` `N = ` `2` `; ` ` ` `int` `D = ` `2` `; ` ` ` `System.out.println(reqString(N, D)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

01100

**Time Complexity:** O(N * D^{N})

**Auxiliary Space:** O(N * D^{N})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Lexicographically smallest K-length substring containing maximum number of vowels
- Distinct permutations of a string containing duplicates using HashSet in Java
- Count of distinct permutations of every possible length of given string
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find smallest possible Number from a given large Number with same count of digits
- Generate all possible permutations of a Number divisible by N
- Minimum digits to be removed to make either all digits or alternating digits same
- Find the smallest window in a string containing all characters of another string
- Python | Get the smallest window in a string containing all characters of given pattern
- Smallest number with given sum of digits and sum of square of digits
- Sum of all numbers that can be formed with permutations of n digits
- Minimize length of prefix of string S containing all characters of another string T
- Check if a binary string contains all permutations of length k
- Print a number containing K digits with digital root D
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Get the kth smallest number using the digits of the given number
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lexicographically smallest and largest anagrams of a string containing another string as its substring

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.