Given a set of characters and a positive integer k, print all possible strings of length k that can be formed from the given set.

**Examples:**

Input: set[] = {'a', 'b'}, k = 3 Output: aaa aab aba abb baa bab bba bbb Input: set[] = {'a', 'b', 'c', 'd'}, k = 1 Output: a b c d

For a given set of size n, there will be n^k possible strings of length k. The idea is to start from an empty output string (we call it *prefix *in following code). One by one add all characters to *prefix*. For every character added, print all possible strings with current prefix by recursively calling for k equals to k-1.

Below is the implementation of above idea :

## Java

`// Java program to print all ` `// possible strings of length k ` ` ` `class` `GFG { ` ` ` `// The method that prints all ` `// possible strings of length k. ` `// It is mainly a wrapper over ` `// recursive function printAllKLengthRec() ` `static` `void` `printAllKLength(` `char` `[] set, ` `int` `k) ` `{ ` ` ` `int` `n = set.length; ` ` ` `printAllKLengthRec(set, ` `""` `, n, k); ` `} ` ` ` `// The main recursive method ` `// to print all possible ` `// strings of length k ` `static` `void` `printAllKLengthRec(` `char` `[] set, ` ` ` `String prefix, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Base case: k is 0, ` ` ` `// print prefix ` ` ` `if` `(k == ` `0` `) ` ` ` `{ ` ` ` `System.out.println(prefix); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// One by one add all characters ` ` ` `// from set and recursively ` ` ` `// call for k equals to k-1 ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `{ ` ` ` ` ` `// Next character of input added ` ` ` `String newPrefix = prefix + set[i]; ` ` ` ` ` `// k is decreased, because ` ` ` `// we have added a new character ` ` ` `printAllKLengthRec(set, newPrefix, ` ` ` `n, k - ` `1` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `System.out.println(` `"First Test"` `); ` ` ` `char` `[] set1 = {` `'a'` `, ` `'b'` `}; ` ` ` `int` `k = ` `3` `; ` ` ` `printAllKLength(set1, k); ` ` ` ` ` `System.out.println(` `"\nSecond Test"` `); ` ` ` `char` `[] set2 = {` `'a'` `, ` `'b'` `, ` `'c'` `, ` `'d'` `}; ` ` ` `k = ` `1` `; ` ` ` `printAllKLength(set2, k); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to print all ` `# possible strings of length k ` ` ` `# The method that prints all ` `# possible strings of length k. ` `# It is mainly a wrapper over ` `# recursive function printAllKLengthRec() ` `def` `printAllKLength(` `set` `, k): ` ` ` ` ` `n ` `=` `len` `(` `set` `) ` ` ` `printAllKLengthRec(` `set` `, "", n, k) ` ` ` `# The main recursive method ` `# to print all possible ` `# strings of length k ` `def` `printAllKLengthRec(` `set` `, prefix, n, k): ` ` ` ` ` `# Base case: k is 0, ` ` ` `# print prefix ` ` ` `if` `(k ` `=` `=` `0` `) : ` ` ` `print` `(prefix) ` ` ` `return` ` ` ` ` `# One by one add all characters ` ` ` `# from set and recursively ` ` ` `# call for k equals to k-1 ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Next character of input added ` ` ` `newPrefix ` `=` `prefix ` `+` `set` `[i] ` ` ` ` ` `# k is decreased, because ` ` ` `# we have added a new character ` ` ` `printAllKLengthRec(` `set` `, newPrefix, n, k ` `-` `1` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `print` `(` `"First Test"` `) ` ` ` `set1 ` `=` `[` `'a'` `, ` `'b'` `] ` ` ` `k ` `=` `3` ` ` `printAllKLength(set1, k) ` ` ` ` ` `print` `(` `"\nSecond Test"` `) ` ` ` `set2 ` `=` `[` `'a'` `, ` `'b'` `, ` `'c'` `, ` `'d'` `] ` ` ` `k ` `=` `1` ` ` `printAllKLength(set2, k) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print all ` `// possible strings of length k ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// The method that prints all ` `// possible strings of length k. ` `// It is mainly a wrapper over ` `// recursive function printAllKLengthRec() ` `static` `void` `printAllKLength(` `char` `[] ` `set` `, ` `int` `k) ` `{ ` ` ` `int` `n = ` `set` `.Length; ` ` ` `printAllKLengthRec(` `set` `, ` `""` `, n, k); ` `} ` ` ` `// The main recursive method ` `// to print all possible ` `// strings of length k ` `static` `void` `printAllKLengthRec(` `char` `[] ` `set` `, ` ` ` `String prefix, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Base case: k is 0, ` ` ` `// print prefix ` ` ` `if` `(k == 0) ` ` ` `{ ` ` ` `Console.WriteLine(prefix); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// One by one add all characters ` ` ` `// from set and recursively ` ` ` `// call for k equals to k-1 ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `{ ` ` ` ` ` `// Next character of input added ` ` ` `String newPrefix = prefix + ` `set` `[i]; ` ` ` ` ` `// k is decreased, because ` ` ` `// we have added a new character ` ` ` `printAllKLengthRec(` `set` `, newPrefix, ` ` ` `n, k - 1); ` ` ` `} ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `Console.WriteLine(` `"First Test"` `); ` ` ` `char` `[] set1 = {` `'a'` `, ` `'b'` `}; ` ` ` `int` `k = 3; ` ` ` `printAllKLength(set1, k); ` ` ` ` ` `Console.WriteLine(` `"\nSecond Test"` `); ` ` ` `char` `[] set2 = {` `'a'` `, ` `'b'` `, ` `'c'` `, ` `'d'` `}; ` ` ` `k = 1; ` ` ` `printAllKLength(set2, k); ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

**Output:**

First Test aaa aab aba abb baa bab bba bbb Second Test a b c d

The above solution is mainly generalization of this post.

This article is contriibuted by **Abhinav Ramana**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- K length words that can be formed from given characters without repetition
- All possible strings of any length that can be formed from a given string
- Number of strings of length N with no palindromic sub string
- Number of Binary Strings of length N with K adjacent Set Bits
- Print all permutations with repetition of characters
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print all increasing sequences of length k from first n natural numbers
- Print all possible strings that can be made by placing spaces
- Print all possible strings that can be made by placing spaces
- length vs length() in Java
- Sum of all numbers that can be formed with permutations of n digits
- Number of triangles that can be formed with given N points
- N digit numbers divisible by 5 formed from the M digits
- Number of triangles formed from a set of points on three lines
- Number of balanced bracket expressions that can be formed from a string