Write a recursive method to print all the permutations of a string. For example,
ID: 3641746 • Letter: W
Question
Write a recursive method to print all the permutations of a string. For example, for a string abc, the printout is:abc
acb
bac
bca
cab
cba
Hint: Define the following two methods. The second is a helper method.
public static void display Permutation (string s)
public static void display Permutation (sting s1, string s2)
The first method simply invokes Permutation (" ",s). The second method uses a loop to move a character from s2 to s1 and recursively invoke it with a new s1 and s2. The base case is that s2 is empty and prints s1 to console.
Explanation / Answer
public static void displayPermutation(String s) { displayPermutation("",s); } public static void displayPermutation(String s1,String s2) { if(s2.length()Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.