0

I'm working on Permutations in Java. My program to is supposed to generate all possible permutations of an array with length n and arrange in r number of permutations.

Here is an example. For an array with these 12 elements

string  Mywords[] =("love" ,"guy" ,"cow","hen","dog","antelope","cat" "rat",welcome","thank","you","all");

Where

n=12, r=7

7 of the 12 elements are selected to create 12 possible variations of the array.

=>> output may be in form (possible case)

  1. (love, guy, cow, hen, dog, antelope, cat)
  2. (love, cow, guy, hen, dog, antelope, cat)
  3. (love, hen, guy, cow, dog, antelope, cat)
  4. (love, dog, hen, guy, cow, antelope, cat)
  5. (love, thank, dog, hen, guy, cow, welcome)
  6. :
  7. :
  8. :
  9. :
  10. :
  11. :
  12. P(n,r)

How can I print all possible results?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Theflier
  • 1
  • 2

1 Answers1

0

The below code is adapted from the answer to this SO question.

print all possible permutations of r elements in a given integer array of size n in Java

That question uses a list of integers. In the below code I essentially only replaced Integer with String.

Note that the number of permutations of 7 elements from a list of 12 elements is close to 3.5 million which will take time. Hence in the below code I generate permutations of three elements from a five element list.

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public static List<List<String>> choose(List<String> a, int k) {
        List<List<String>> allPermutations = new ArrayList<List<String>>();
        enumerate(a, a.size(), k, allPermutations);
        return allPermutations;
    }

    // a is the original array
    // n is the array size
    // k is the number of elements in each permutation
    // allPermutations is all different permutations
    private static void enumerate(List<String> a,
                                  int n,
                                  int k,
                                  List<List<String>> allPermutations) {
        if (k == 0) {
            List<String> singlePermutation = new ArrayList<>();
            for (int i = n; i < a.size(); i++) {
                singlePermutation.add(a.get(i));
            }
            allPermutations.add(singlePermutation);
            return;
        }
        for (int i = 0; i < n; i++) {
            swap(a, i, n - 1);
            enumerate(a, n - 1, k - 1, allPermutations);
            swap(a, i, n - 1);
        }
    }

    // helper function that swaps a.get(i) and a.get(j)
    public static void swap(List<String> a, int i, int j) {
        String temp = a.get(i);
        a.set(i, a.get(j));
        a.set(j, temp);
    }

    // sample client
    public static void main(String[] args) {

        // create original array
        List<String> elements = new ArrayList<>(List.of("love",
                                                        "guy",
                                                        "cow",
                                                        "hen",
                                                        "dog"));
        int n = elements.size();

        // k is the number of elements of each permutation.
        int k = 3;

        List<String> a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            a.add(elements.get(i));
        }
        choose(a, k).forEach(list -> System.out.println(list));
    }
}

Running the above code produces the following output.

[hen, dog, love]
[guy, dog, love]
[cow, dog, love]
[dog, guy, love]
[hen, guy, love]
[cow, guy, love]
[dog, cow, love]
[guy, cow, love]
[hen, cow, love]
[dog, hen, love]
[guy, hen, love]
[cow, hen, love]
[hen, love, guy]
[dog, love, guy]
[cow, love, guy]
[love, dog, guy]
[hen, dog, guy]
[cow, dog, guy]
[love, cow, guy]
[dog, cow, guy]
[hen, cow, guy]
[love, hen, guy]
[dog, hen, guy]
[cow, hen, guy]
[hen, love, cow]
[guy, love, cow]
[dog, love, cow]
[love, guy, cow]
[hen, guy, cow]
[dog, guy, cow]
[love, dog, cow]
[guy, dog, cow]
[hen, dog, cow]
[love, hen, cow]
[guy, hen, cow]
[dog, hen, cow]
[dog, love, hen]
[guy, love, hen]
[cow, love, hen]
[love, guy, hen]
[dog, guy, hen]
[cow, guy, hen]
[love, cow, hen]
[guy, cow, hen]
[dog, cow, hen]
[love, dog, hen]
[guy, dog, hen]
[cow, dog, hen]
[hen, love, dog]
[guy, love, dog]
[cow, love, dog]
[love, guy, dog]
[hen, guy, dog]
[cow, guy, dog]
[love, cow, dog]
[guy, cow, dog]
[hen, cow, dog]
[love, hen, dog]
[guy, hen, dog]
[cow, hen, dog]
Abra
  • 19,142
  • 7
  • 29
  • 41