You have been given two integers ‘N’ and ‘K’. Consider a set ‘X’ of all possible strings of ‘N’ number of digits such that all strings contain digits only in range 0 to ‘K’ inclusive.
For example, If ‘N’ is 2 and ‘K’ is ‘2’ then the set ‘X’ will be-
‘X’ = { 00, 01, 02, 10, 11, 12, 20, 21, 22 }.
Your task is to find a string of minimum possible length such that it contains all strings from set ‘X as any of its substring.
Note:
If there are more than one possible such strings, then you can output any of them.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains two space-separated integers ‘N’ and ‘K’ representing the number of digits in a number and upper range of possible digits respectively.
Output Format:
For each test case, the output will be 1 if you have returned the correct string, else 0.
The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 4
0 <= K <= 9
Where ‘T’ is the total number of test cases, ‘N’ and ‘K’ are the given integers.
Time Limit: 1 sec
2
2 1
1 0
00110
0
Test case 1:
Total possible 2-digit strings that have {0, 1} digits only are {00, 01, 10,11}, the string ‘00110’ contains all these numbers as a substring and has the minimum possible size, ‘11001’ is also a correct string.
Test case 2:
Only possible string is 0 so the string with the minimum possible size is ‘0’ itself.
1
1 1
10
Test case 1:
The total possible 1-digit numbers are 1 and 0, the string ‘10’ is the smallest string that contains both these strings as a substring.
String ‘01’ is also a valid answer.
Try to think of a graph structure of the problem.
We need to find a ‘S’ string that contains all possible permutations of length ‘N’ and allowed characters ‘0’ to ‘K’. So there can be total ‘(K + 1) ^ N’ possible permutations, For example if ‘N’ is 2 and ‘K’ is 1 then total possible permutations are { 00, 01, 10, 11} i.e 2 ^ 2 = 4 total possible. We can simply concatenate each one to find such ‘S’ i.e ‘S’ = “00011011”, but in order to minimise the length of ‘S we need to make sure that every permutation occurs exactly once in the ‘S’. Such strings are made from De Bruijn sequence. We can visualise the given Problem as a directed graph structure where each node represents distinct possible permutations of length ‘N’ and a edge from ‘node1’ to ‘node2’ represents that we can add a digit in range [0, k] to the suffix of length ‘N - 1’ of ‘node1’ to make ‘node2’.
For example for ‘N’ = 2 and ‘K’ = 2, the graph structure will look like below.

Now we just need to find a Hamiltonian Path in the above graph and add the character of edges in the path to the starting string. For example, let's start with ‘00’ and initialize our answer with ‘00’.
We can run a dfs with a backtrack to find such a path.
This is a boolean function that returns true if we find a valid path by traversing an edge, else return false.
The function will take four-parameter:
‘N’: The given length of numbers
‘K’: allowed range of digits
‘answer’: global answer string
‘visited’: A hashmap to keep track of visited nodes.
O(N * ((K + 1) ^ N) ) where ‘N’ and ‘K’ are the given integers.
As there are (‘K’ + 1) ^ ‘N’ total possible permutations and we are iterating over each of them exactly once that takes O((K + 1) ^ N) time. Also, we are maintaining a hashmap/ unordered set of strings of size ‘N’ to keep track of visited permutation, which takes O( size of string) time for lookup and insertions. Hence the overall time complexity is O(N * ((K + 1) ^ N) ).
O(N * ((K + 1) ^ N) ) where ‘N’ and ‘K’ are the given integers.
As we are using a hashmap to store total possible permutations that is (‘K’ + 1) ^ ‘N’ in the count, that takes O(N * ((K + 1) ^ N) ) space. Also, we are calling recursive functions so in the worst case, the recursion stack will store (K + 1) ^ N function calls that take O((K + 1) ^ N) space. Hence the overall space complexity is O(N * ((K + 1) ^ N) )