Question: complete the code where it says //your code Problem 4: All Permutations (25) Description: Given a set of characters and a positive integer k, print

 complete the code where it says "//your code" Problem 4: All
Permutations (25) Description: Given a set of characters and a positive integer
k, print all possible strings of length k that can be formed
complete the code where it says "//your code"

Problem 4: All Permutations (25) Description: 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. Please implement it using recursion. Examples: Input: char[] = {'a', 'b'}, k = 3 Output: aaa ajab aba abb baa bab bba bbb Input: char[] = {'a', 'b', 'c','d'], k = 1 Output: a b d Hints: In addition to the getAllPermutations() method, you can introduce another recursive helper method to help you find all permutations. Suppose its header is: static void getStringsRec(ArrayList list, char[] chars, String prefix, int k) The basic idea is suppose we have a string, suppose it is named prefix, and at the very beginning it is an empty string. We can then go through the char[array, and in each iteration, concatenate one element which is a single character to the string prefix. Decrease k by 1 every time we have concatenate one character until k reaches O, this indicates one permutation, and we can add this string into the resulting ArrayList. Base case: ke=0. One permutation found. Add it into the resulting ArrayList. Recursive case: k>0: for (int i=0; i getAllPermutations (char[] a, int k){ 7/ YOUR CODES return null; // For compilation. You need to change it. } public static void main(String[] args) { char[] a('a''b', 'c','d'); System.out.println("All Permutations: + getAllPermutations (a, 2)); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!