Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the String

Hard Acceptance 72.98% Points 40.00

Given two integers N and K, the task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string should be from integers ranging from 0 to K-1. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.

Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.

Examples
Example 1

Example 1

Input: N = 2, K = 2

Output: 00110

Explanation: Allowed characters are from 0 to k-1 (i.e., 0 and 1). There are 4 string possible of size N=2 (i.e "00", "01","10","11"). "00110" contains all possible string as a substring. It also has the minimum length.

Hints
Hint 1
Expected Time Complexity: O(KNK)
Hint 2
Expected Space Complexity: O(KNN)
Constraints
  • 1 <= N <= 4
  • 1 < K < 10
  • 1 < KNN < 10^6
Companies
Google
Topics
String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions