-1

{A, B, C, D, E, F} How can I list all possible different orders of these 6 letters?

Tried randomly generating a string such as "ABDFEC" etc. and added to list, if it is already in list skipped that one and tried again and again and again. I was able to find 600≈700 of them but unfortunately it stuck at these because it can't find anything different than the previous ones.

So never mind this stupid idea. What is your brilliant one? Help me out please

  • Check out the itertools module: https://docs.python.org/3/library/itertools.html And please post your solution as an answer. – Joooeey Apr 04 '18 at 16:35
  • This is what you need: https://gist.github.com/axelpale/3118596 – Dat Nguyen Apr 04 '18 at 16:36
  • 3
    Possible duplicate of [Generating all permutations of a given string](https://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string) – eesiraed Apr 04 '18 at 16:53
  • 1
    There's a difference between combinations and permutations. I think you want permutations. – Jim Mischel Apr 04 '18 at 22:03

4 Answers4

1

This can be achieved by backtracking. Please refer the link program to print all permutations of a given string for detailed information of the below code.

public class Permutation
{
    public static void main(String[] args)
    {
        String str = "ABC";
        int n = str.length();
        Permutation permutation = new Permutation();
        permutation.permute(str, 0, n-1);
    }

    /**
     * permutation function
     * @param str string to calculate permutation for
     * @param l starting index
     * @param r end index
     */
    private void permute(String str, int l, int r)
    {
        if (l == r)
            System.out.println(str);
        else
        {
            for (int i = l; i <= r; i++)
            {
                str = swap(str,l,i);
                permute(str, l+1, r);
                str = swap(str,l,i);
            }
        }
    }

    /**
     * Swap Characters at position
     * @param a string value
     * @param i position 1
     * @param j position 2
     * @return swapped string
     */
    public String swap(String a, int i, int j)
    {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }

}

Output:

ABC
ACB
BAC
BCA
CBA
CAB
Subash J
  • 2,028
  • 3
  • 13
  • 27
0

This can be done by using backtracking algorithm

Algorithm

help
  • 5
  • 4
0

You can try to go with something like this:

public static ArrayList<String> combinations = new ArrayList<>();


public static void main(String[] args) {
    char[] letters = {'A', 'B', 'C', 'D', 'E', 'F'};
    computePermutation(letters, 0, letters.length);



    Iterator<String> it = combinations.iterator();
    while(it.hasNext())System.out.println(it.next());
    System.out.println(combinations.size());
}

private static void computePermutation(char[] current, int startIndex, int endIndex) {
    // Base case
    if (startIndex == endIndex) {
        combinations.add(new String(current));
    } else {
        //try to move the swap window from start index to end index
        //i.e 0 to a.length-1
        for (int x = startIndex; x < endIndex; x++) {
            swap(current, startIndex, x);
            computePermutation(current, startIndex + 1, endIndex);
            swap(current, startIndex, x);
        }
    }
}

private static void swap(char[] a, int i, int x) {
    char t = a[i];
    a[i] = a[x];
    a[x] = t;
}

Also refer to this question from StackOverflow since it is almost the exact same thing: Generating all permutation of a character array

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
0

In python, we can use itertools.permutations() to solve this.

>>> import itertools
>>> x = itertools.permutations([1,2,3])
>>> for i in x:
       print i

The result will be as below.

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
nghiep truong
  • 310
  • 3
  • 8