JAVA B04 - Number/String permutation Write a program that will ask the user for
ID: 3915035 • Letter: J
Question
JAVA
B04 - Number/String permutation Write a program that will ask the user for a number, and then it will compute the permutation of every digit in that number. You need a recursive method that will print all the permutations of the number. For example, for the number 123, the permutation is:
123
132
213
231
312
321
Hint: Define two methods. The second is a helper method.
public static void permutation(String s)
public static void permutation(String s1, String s2)
The first method simply invokes displayPermutation(" ", s).The second method uses a loop to move a character from s2 to s1 and recursively invokes it with a new s1 and s2. The base case is that s2 is empty and prints s1 to the console.
Write a test program that prompts the user to enter a number and displays all its permutations.
Explanation / Answer
public class Permutation { public static void permutation(String s) { permutation("", s); } public static void permutation(String s1, String s2) { if(s2.isEmpty()) { System.out.println(s1); } else { for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.