Smallest number containing all possible N length permutations using digits 0 to D | Initialize set to see if all the possible permutations are present in the min length string ; To keep min length string ; Generate the required string ; Iterate over all possible character ; Append to make a new string ; If the new string is not visited ; Add in set ; Call the dfs function on the last d characters ; Base case ; Append '0' n - 1 times ; Call the DFS Function ; Driver Code
visited = set ( ) NEW_LINE ans = [ ] NEW_LINE def dfs ( curr , D ) : NEW_LINE INDENT for c in range ( D ) : NEW_LINE INDENT c = str ( c ) NEW_LINE neighbour = curr + c NEW_LINE if neighbour not in visited : NEW_LINE INDENT visited . add ( neighbour ) NEW_LINE dfs ( neighbour [ 1 : ] , D ) NEW_LINE ans . append ( c ) NEW_LINE DEDENT DEDENT DEDENT def reqString ( N , D ) : NEW_LINE INDENT if ( N == 1 and D == 1 ) : NEW_LINE INDENT return "0" NEW_LINE DEDENT start = ' ' . join ( [ '0' ] * ( N - 1 ) ) NEW_LINE dfs ( start , D ) NEW_LINE ans . extend ( [ '0' ] * ( N - 1 ) ) NEW_LINE return ' ' . join ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , D = 2 , 2 NEW_LINE print ( reqString ( N , D ) ) NEW_LINE DEDENT