// Permutation of a String with duplicates
/**
* @author Prateek
*/
class Permutation{
private char[] str=null; // to hold string data
/**
* Takes the initial input
*/
public void printPermutations
(String text
){ str=text.toCharArray();
permutate(0,text.length()-1);
}
/**
* Subroutine to compute permutations
* @param index
* @param len
*/
private void permutate( int index, int len )
{
if( index == len )
for(int i = index; i <= len; i++ )
{
if(!match(index, i)){
swap(index, i );
permutate(index + 1 , len);
swap( index, i );
}
}
}
/**
* to check if any character matches between i and j
* @return if any characted match found
*/
private boolean match(int i, int j) {
if(i == j)
return false;
else
for(;i<j ; i++)
if(str[i]==str[j])
return true;
return false;
}
public static void main
(String[] a
) { Permutation obj=new Permutation();
obj.printPermutations(text) ;
}
private void swap(int a, int b) {
char temp=str[a];
str[a]=str[b];
str[b]=temp;
}
}
Ly8gUGVybXV0YXRpb24gb2YgYSBTdHJpbmcgd2l0aCBkdXBsaWNhdGVzCi8qKgogKiBAYXV0aG9yIFByYXRlZWsKICovCiBjbGFzcyBQZXJtdXRhdGlvbnsKCglwcml2YXRlIGNoYXJbXSBzdHI9bnVsbDsgLy8gdG8gaG9sZCBzdHJpbmcgZGF0YQoKCS8qKgoJICogVGFrZXMgdGhlIGluaXRpYWwgaW5wdXQKCSAqLwoJcHVibGljIHZvaWQgcHJpbnRQZXJtdXRhdGlvbnMoU3RyaW5nIHRleHQpewoJCXN0cj10ZXh0LnRvQ2hhckFycmF5KCk7CgkJcGVybXV0YXRlKDAsdGV4dC5sZW5ndGgoKS0xKTsKCX0KCQoJLyoqCgkgKiBTdWJyb3V0aW5lIHRvIGNvbXB1dGUgcGVybXV0YXRpb25zCgkgKiBAcGFyYW0gaW5kZXgKCSAqIEBwYXJhbSBsZW4KCSAqLwoJcHJpdmF0ZSB2b2lkIHBlcm11dGF0ZSggaW50IGluZGV4LCBpbnQgbGVuICkKCXsKCQlpZiggaW5kZXggPT0gbGVuICkKCQkJU3lzdGVtLm91dC5wcmludGxuKChzdHIpKTsKCgkJZm9yKGludCBpID0gaW5kZXg7IGk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