A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
Source: Mathword(http://mathworld.wolfram.com/Permutation.html)

Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB

Here is a solution that is used as a basis in backtracking.

java

 

 

 

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20print%20all%20permutations%20of%20a%0A%2F%2F%20given%20string.%0Apublic%20class%20Permutation%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20String%20str%20%3D%20%22ABC%22%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20str.length()%3B%0A%20%20%20%20%20%20%20%20Permutation%20permutation%20%3D%20new%20Permutation()%3B%0A%20%20%20%20%20%20%20%20permutation.permute(str%2C%200%2C%20n-1)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20permutation%20function%0A%20%20%20%20%20*%20%40param%20str%20string%20to%20calculate%20permutation%20for%0A%20%20%20%20%20*%20%40param%20l%20starting%20index%0A%20%20%20%20%20*%20%40param%20r%20end%20index%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20void%20permute(String%20str%2C%20int%20l%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(l%20%3D%3D%20r)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(str)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%20l%3B%20i%20%3C%3D%20r%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20str%20%3D%20swap(str%2Cl%2Ci)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20permute(str%2C%20l%2B1%2C%20r)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20str%20%3D%20swap(str%2Cl%2Ci)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20Swap%20Characters%20at%20position%0A%20%20%20%20%20*%20%40param%20a%20string%20value%0A%20%20%20%20%20*%20%40param%20i%20position%201%0A%20%20%20%20%20*%20%40param%20j%20position%202%0A%20%20%20%20%20*%20%40return%20swapped%20string%0A%20%20%20%20%20*%2F%0A%20%20%20%20public%20String%20swap(String%20a%2C%20int%20i%2C%20int%20j)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20char%20temp%3B%0A%20%20%20%20%20%20%20%20char%5B%5D%20charArray%20%3D%20a.toCharArray()%3B%0A%20%20%20%20%20%20%20%20temp%20%3D%20charArray%5Bi%5D%20%3B%0A%20%20%20%20%20%20%20%20charArray%5Bi%5D%20%3D%20charArray%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20charArray%5Bj%5D%20%3D%20temp%3B%0A%20%20%20%20%20%20%20%20return%20String.valueOf(charArray)%3B%0A%20%20%20%20%7D%0A%20%0A%7D” message=”Java” highlight=”” provider=”manual”/]

 

Output:

ABC
ACB
BAC
BCA
CBA
CAB


Algorithm Paradigm:
Backtracking
Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a a permutation.

[ad type=”banner”]