Finding all the permutations of a string is by a well known Steinhaus–Johnson–Trotter algorithm. But if the string contains the repeated characters such as
AABB,
the
This is a tricky question and we need to use recursion to find all the permutations of a String, for example "AAB" permutations will be "AAB", "ABA" and "BAA". We also need to use Set to make sure there are no duplicate values.
import java.io.*;
import java.util.HashSet;
import java.util.*;
class Permutation {
static HashSet set = new HashSet();
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter :");
StringBuilder str = new StringBuilder(in.nextLine());
NONDuplicatePermutation("",str.toString()); //WITHOUT DUPLICATE PERMUTATION OF STRING
System.out.println(set);
}
public static void NONDuplicatePermutation(String prefix,String str){
//It is nlogn
if(str.length()==0){
set.add(prefix);
}else{
for(int i=0;i