Reputation: 7728
I know that this question has been answered before on stackoverflow, however I am asking this not to tell me the correct code, but because I want to know what I am doing wrong.
public static void printCombinations(String str){
printCombinations(str, 0, str.length()-1);
}
public static void printCombinations(String str,int k,int n){
if(k == n)
System.out.println(str);
else {
for(int i=k;i<n;i++){
String tmp=modifyString(str,i,k);
printCombinations(tmp,k+1,n);
modifyString(str,i,k);
}
}
}
public static String modifyString(String str,int x,int y){
// for swapping characters inside a string
char arr[]=str.toCharArray();
char t= arr[x];
arr[x]=arr[y];
arr[y]=t;
String s= new String(arr);
return s;
}
I am calling the function as printCombinations(s)
.
Upvotes: 3
Views: 5827
Reputation: 779
this worked for me..
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
public class StringPermutations{
private static Set<String> uniqueResultSet = new HashSet<String>();
public static void main(String args[]) {
String inputString = "ABC";
permute(inputString.toCharArray(), 0, inputString.length()-1);
System.out.println(uniqueResultSet);
}
public static void permute(char[] ary, int startIndex, int endIndex) {
if(startIndex == endIndex){
uniqueResultSet.add(String.valueOf(ary));
}else{
for(int i=startIndex;i<=endIndex;i++) {
swap(ary, startIndex, i );
permute(ary, startIndex+1, endIndex);
swap(ary, startIndex, i );
}
}
}
public static void swap(char[] ary, int x, int y) {
char temp = ary[x];
ary[x] = ary[y];
ary[y] = temp;
}
}
Upvotes: 0
Reputation: 779
I'm considering @CSSS code and made some changes so that it is very much clear to beginners. you just need to dump this code and you can run it.I't working correctly.Thanks to @CSSS. The code of @CSSS providing unnecessary duplication permutations,in order to avoid this I used HashSet,for each iteration,I'm adding respective permutation to the HashSet. As we know Set implementation does not allows duplications.So it is giving correct result.In MyClass
class Myclass
{
static Set<String> resultSet=new HashSet<String>();
public static void main(String[] args){
String str=new Scanner(System.in).next();
printCombinations(str, 0, str.length(),resultSet);
Object[] finalArray=resultSet.toArray();
int i=1;
for(Object s:finalArray){
System.out.println(s.toString()+"\t"+i++);
}
}
public static void printCombinations(String str,int k,int n,Set<String> resultSet){
for(int i=k;i<n;i++){
String temp=modifyString(str,i,k);
resultSet.add(temp);
printCombinations(temp,k+1,n,resultSet);
}
}
public static String modifyString(String str,int x,int y){
char arr[]=str.toCharArray();
char t= arr[x];
arr[x]=arr[y];
arr[y]=t;
String s= new String(arr);
return s;
}
}
Upvotes: 1
Reputation: 3627
The function modifyString
doesn't modify a string it returns a modified String - maybe you want to rename it.
You call it twice - maybe the second time something should be done with the output?
What do you want to achieve?
Upvotes: 0