简体   繁体   中英

All possible combinations of a string with no repetitions in Java

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) .

change

i<n to i<=n

it should work.

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;   
    }

}

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?

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM