Strings of Numbers

Hard
0/120
Average time to solve is 45m
profile
Contributed by
5 upvotes
Asked in companies
AppleFacebookUber

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )

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

Sample Input 1:

2
2 1
1 0

Sample Output 1:

00110
0

Explanation of Sample Input 1:

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.

Sample Input 2:

1
1 1

Sample Output 2:

10

Explanation of Sample Input 2:

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.
Hint

Try to think of a graph structure of the problem.

Approaches (2)
Recursive DFS

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 move to ‘01’ and add ‘1’ to answer.
  • Now we will move to ‘11’ because if we move to ‘10’, we got stuck, so add ‘1’ to answer.
  • Now from ‘11’ we move to ‘10’, thus completing our path, and hence the answer becomes ‘00110’.

We can run a dfs with a backtrack to find such a path.

 

Algorithm:

 

  • Let the given integers be ‘N’ and ‘K
  • Declare a function dfs to visit all nodes in the graph once.
  • Declare a hashmap of string say ‘visited’ to keep track of the visited strings (nodes) in the graph.
  • Declare a string ‘answer’ and initialize it with ‘000… N times’.
  • Insert ‘answer’ into ‘visited’.
  • Call the dfs function to find a hamiltonian path.
  • Return ‘answer’.

Description of ‘dfs’ function

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.

 

bool dfs ( N, K, answer, visited ):

 

  • If we have visited all the nodes i.e. the size of ‘visited’ is (‘K’ + 1) ^ ‘N’
    • return true.
  • Run a loop from ‘i’ = 0 to ‘K
    • Declare a string say ‘currNode’ and set it to substring ‘answer(1, N-1)’.
    • Add string of ‘i’ to ‘currNode’.
    • If ‘currNode’ is not visited.
      • Insert ‘currNode’ into ‘visited’.
      • Check if adding ‘i’ to 'answer' is a valid path or not i.e If dfs( ‘answer’ + ‘str(i)’) == true then
        • Return true.
      • Else:
        • Remove ‘currNode’ from ‘visited’, as we cannot add ‘i’ to ‘answer’ now.
  • Return false.
Time Complexity

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) ).

Space Complexity

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) )

Code Solution
(100% EXP penalty)
Strings of Numbers
Full screen
Console