All N Length Strings from Given String of Length K

Objective: Print All N Length Strings from Given String of Length K where characters can appear multiple time.

Example:

String k = "ALGO"
N=2

Result:
AA LA GA OA AL LL GL OL AG LG GG OG AO LO GO OO

 

Approach:

This problem is quite similar to Print All N Length Strings from Given Number K.

  • Loop through i = 1 to K.
  • Add k[i] to the result Array, which is the size N and make a recursive call to (N-1).
  • Base case: when n becomes 0 (means array is full).
  • See the code for better explanation.

Code:

Output:

AA LA GA OA AL LL GL OL AG LG GG OG AO LO GO OO

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

  • Huy Hoang-Nguyen

    Great solution. How did you come up with it?
    Looks like a DP solution will work for this problem.

%d bloggers like this: