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

1 thought on “All N Length Strings from Given String of Length K”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.