# 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:

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 public class NLengthStringFromKLength { public void print(int n, char[] k, char[] A) { if (n <= 0) { System.out.print(String.valueOf(A) + " "); } else { for (int i = 0; i < k.length; i++) { A[n – 1] = k[i]; print(n – 1, k, A); } } } public static void main(String[] args) { String k = "ALGO"; int n = 2; NLengthStringFromKLength i = new NLengthStringFromKLength(); i.print(n, k.toCharArray(), new char[n]); } }

```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”

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