Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

All N Length Strings from Given String of Length K

Objec­tive: Print All N Length Strings from Given String of Length K where char­ac­ters can appear mul­ti­ple time.

Exam­ple:

String k = "ALGO"
N=2

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

 

Approach:

This prob­lem is quite sim­i­lar to Print All N Length Strings from Given Num­ber K.

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

Code:

Output:

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

You may also like...

  • Huy Hoang-Nguyen

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