For this assignment you are to write a program that takes a
For this assignment you are to write a program that takes a string as input, and print out all permutations of the characters in that string. using loops
Solution
package backtracking;
public class StringPermutationWithoutRepetition
{
public static void main(String[] args)
{
permutation(\"ABC\");
}
private static void permutation(String string)
{
printPermutation(string,\"\");
}
private static void printPermutation(String string, String permutation)
{
if(string.length()==0)
{
System.out.println(permutation);
return;
}
for (int i = 0; i < string.length(); i++)
{
char toAppendToPermutation = string.charAt(i);
String remaining = string.substring(0, i) + string.substring(i + 1);
printPermutation( remaining, permutation + toAppendToPermutation);
}
}
}
Now the output will be like this in the given below:
Case 1
Input: A
output: A
Case 2
Input: AB
output:
AB
BA
Case 3
Input: ABC
output:
ABC
ACB
BAC
BCA
CAB
CBA

