Get all possible combinations of two array of string










0















I have a two string array:



A("0", "1", "2", "3", "4", "5", "6", "7")
B("a", "b", "c", "d", "e")


Permutation to discover the amount of possible combinations:



[((8!)/(8-5)!)*((3!)/(3-2)!)]*[(7!)/((2!)*(7-2)!)]
40320 * 21 = 846720


How can I get all the combinations between the two arrays, using 5 elements of A and 2 elements of B, without repetitions?



For this, I made a code to retrieve all the "combination keys":



package wodlist;

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

public class GenerateKey

static void perm1(String c0, int n0, String c1, int n1, String s,
List<String> result)

static List<String> perm(String c0, int n0, String c1, int n1)
List<String> result = new ArrayList<>();
perm1(c0, n0, c1, n1, "", result);
return result;




When calling the function perm("A", 5, "B", 2) I will have a result similar to this::



[AAAAABB, AAAABAB, AAAABBA, AAABAAB, AAABABA, AAABBAA, AABAAAB, AABAABA, AABABAA, AABBAAA, ABAAAAB, ABAAABA, ABAABAA, ABABAAA, ABBAAAA, BAAAAAB, BAAAABA, BAAABAA, BAABAAA, BABAAAA, BBAAAAA]


This is the "key", but how can I get all combinations of each key using 5 elements of A and 2 elements of B?



For example:



AAAAABB = 0,2,3,4,5,a,b, 0,2,3,4,5,a,c, 0,2,3,4,5,a,d...
AAAABAB = ...


I made this example, which has the same "logic", but I can not replicate with it, since in it I knew the amount of possible combinations. Where I have both arrays, the amount of characters I will use each, but the problem of this with the other is that I know the number of possible combinations of each "key." Something I do not know about the problem above.



 String A = new String"1","2","3";
String B = new String"a","b","c";
//key
String AAB = new String[18];
String ABA = new String[18];
String BAA = new String[18];
//result
String S = new String[54];
//
//[A0,A1,B]
int aabIndex = 0, abaIndex = 0, baaIndex=0;
for (int a0Index = 0; a0Index < 3; a0Index++)
for (int a1Index = 0; a1Index < 3; a1Index++)
// skip when A0 == A1
if (a0Index == a1Index) continue;
// scroll through b
for(int bIndex = 0; bIndex < 3; bIndex++)
AAB[aabIndex++] = A[a0Index] + A[a1Index] + B[bIndex];
ABA[abaIndex++] = A[a0Index] + B[bIndex] + A[a1Index];
BAA[baaIndex++] = B[bIndex] + A[a0Index] + A[a1Index];





Permutation to arrive at the above result:



[Arrangement(3,2)*Arrangement(3,1)]*Combination(3,2)
[(3!/(3-2)!)*(3!/(3-1)!]*[3!/(2!*(3-2)!) =
[6 * 3] * 3 = 54


Can anybody help me?










share|improve this question


























    0















    I have a two string array:



    A("0", "1", "2", "3", "4", "5", "6", "7")
    B("a", "b", "c", "d", "e")


    Permutation to discover the amount of possible combinations:



    [((8!)/(8-5)!)*((3!)/(3-2)!)]*[(7!)/((2!)*(7-2)!)]
    40320 * 21 = 846720


    How can I get all the combinations between the two arrays, using 5 elements of A and 2 elements of B, without repetitions?



    For this, I made a code to retrieve all the "combination keys":



    package wodlist;

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

    public class GenerateKey

    static void perm1(String c0, int n0, String c1, int n1, String s,
    List<String> result)

    static List<String> perm(String c0, int n0, String c1, int n1)
    List<String> result = new ArrayList<>();
    perm1(c0, n0, c1, n1, "", result);
    return result;




    When calling the function perm("A", 5, "B", 2) I will have a result similar to this::



    [AAAAABB, AAAABAB, AAAABBA, AAABAAB, AAABABA, AAABBAA, AABAAAB, AABAABA, AABABAA, AABBAAA, ABAAAAB, ABAAABA, ABAABAA, ABABAAA, ABBAAAA, BAAAAAB, BAAAABA, BAAABAA, BAABAAA, BABAAAA, BBAAAAA]


    This is the "key", but how can I get all combinations of each key using 5 elements of A and 2 elements of B?



    For example:



    AAAAABB = 0,2,3,4,5,a,b, 0,2,3,4,5,a,c, 0,2,3,4,5,a,d...
    AAAABAB = ...


    I made this example, which has the same "logic", but I can not replicate with it, since in it I knew the amount of possible combinations. Where I have both arrays, the amount of characters I will use each, but the problem of this with the other is that I know the number of possible combinations of each "key." Something I do not know about the problem above.



     String A = new String"1","2","3";
    String B = new String"a","b","c";
    //key
    String AAB = new String[18];
    String ABA = new String[18];
    String BAA = new String[18];
    //result
    String S = new String[54];
    //
    //[A0,A1,B]
    int aabIndex = 0, abaIndex = 0, baaIndex=0;
    for (int a0Index = 0; a0Index < 3; a0Index++)
    for (int a1Index = 0; a1Index < 3; a1Index++)
    // skip when A0 == A1
    if (a0Index == a1Index) continue;
    // scroll through b
    for(int bIndex = 0; bIndex < 3; bIndex++)
    AAB[aabIndex++] = A[a0Index] + A[a1Index] + B[bIndex];
    ABA[abaIndex++] = A[a0Index] + B[bIndex] + A[a1Index];
    BAA[baaIndex++] = B[bIndex] + A[a0Index] + A[a1Index];





    Permutation to arrive at the above result:



    [Arrangement(3,2)*Arrangement(3,1)]*Combination(3,2)
    [(3!/(3-2)!)*(3!/(3-1)!]*[3!/(2!*(3-2)!) =
    [6 * 3] * 3 = 54


    Can anybody help me?










    share|improve this question
























      0












      0








      0


      0






      I have a two string array:



      A("0", "1", "2", "3", "4", "5", "6", "7")
      B("a", "b", "c", "d", "e")


      Permutation to discover the amount of possible combinations:



      [((8!)/(8-5)!)*((3!)/(3-2)!)]*[(7!)/((2!)*(7-2)!)]
      40320 * 21 = 846720


      How can I get all the combinations between the two arrays, using 5 elements of A and 2 elements of B, without repetitions?



      For this, I made a code to retrieve all the "combination keys":



      package wodlist;

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

      public class GenerateKey

      static void perm1(String c0, int n0, String c1, int n1, String s,
      List<String> result)

      static List<String> perm(String c0, int n0, String c1, int n1)
      List<String> result = new ArrayList<>();
      perm1(c0, n0, c1, n1, "", result);
      return result;




      When calling the function perm("A", 5, "B", 2) I will have a result similar to this::



      [AAAAABB, AAAABAB, AAAABBA, AAABAAB, AAABABA, AAABBAA, AABAAAB, AABAABA, AABABAA, AABBAAA, ABAAAAB, ABAAABA, ABAABAA, ABABAAA, ABBAAAA, BAAAAAB, BAAAABA, BAAABAA, BAABAAA, BABAAAA, BBAAAAA]


      This is the "key", but how can I get all combinations of each key using 5 elements of A and 2 elements of B?



      For example:



      AAAAABB = 0,2,3,4,5,a,b, 0,2,3,4,5,a,c, 0,2,3,4,5,a,d...
      AAAABAB = ...


      I made this example, which has the same "logic", but I can not replicate with it, since in it I knew the amount of possible combinations. Where I have both arrays, the amount of characters I will use each, but the problem of this with the other is that I know the number of possible combinations of each "key." Something I do not know about the problem above.



       String A = new String"1","2","3";
      String B = new String"a","b","c";
      //key
      String AAB = new String[18];
      String ABA = new String[18];
      String BAA = new String[18];
      //result
      String S = new String[54];
      //
      //[A0,A1,B]
      int aabIndex = 0, abaIndex = 0, baaIndex=0;
      for (int a0Index = 0; a0Index < 3; a0Index++)
      for (int a1Index = 0; a1Index < 3; a1Index++)
      // skip when A0 == A1
      if (a0Index == a1Index) continue;
      // scroll through b
      for(int bIndex = 0; bIndex < 3; bIndex++)
      AAB[aabIndex++] = A[a0Index] + A[a1Index] + B[bIndex];
      ABA[abaIndex++] = A[a0Index] + B[bIndex] + A[a1Index];
      BAA[baaIndex++] = B[bIndex] + A[a0Index] + A[a1Index];





      Permutation to arrive at the above result:



      [Arrangement(3,2)*Arrangement(3,1)]*Combination(3,2)
      [(3!/(3-2)!)*(3!/(3-1)!]*[3!/(2!*(3-2)!) =
      [6 * 3] * 3 = 54


      Can anybody help me?










      share|improve this question














      I have a two string array:



      A("0", "1", "2", "3", "4", "5", "6", "7")
      B("a", "b", "c", "d", "e")


      Permutation to discover the amount of possible combinations:



      [((8!)/(8-5)!)*((3!)/(3-2)!)]*[(7!)/((2!)*(7-2)!)]
      40320 * 21 = 846720


      How can I get all the combinations between the two arrays, using 5 elements of A and 2 elements of B, without repetitions?



      For this, I made a code to retrieve all the "combination keys":



      package wodlist;

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

      public class GenerateKey

      static void perm1(String c0, int n0, String c1, int n1, String s,
      List<String> result)

      static List<String> perm(String c0, int n0, String c1, int n1)
      List<String> result = new ArrayList<>();
      perm1(c0, n0, c1, n1, "", result);
      return result;




      When calling the function perm("A", 5, "B", 2) I will have a result similar to this::



      [AAAAABB, AAAABAB, AAAABBA, AAABAAB, AAABABA, AAABBAA, AABAAAB, AABAABA, AABABAA, AABBAAA, ABAAAAB, ABAAABA, ABAABAA, ABABAAA, ABBAAAA, BAAAAAB, BAAAABA, BAAABAA, BAABAAA, BABAAAA, BBAAAAA]


      This is the "key", but how can I get all combinations of each key using 5 elements of A and 2 elements of B?



      For example:



      AAAAABB = 0,2,3,4,5,a,b, 0,2,3,4,5,a,c, 0,2,3,4,5,a,d...
      AAAABAB = ...


      I made this example, which has the same "logic", but I can not replicate with it, since in it I knew the amount of possible combinations. Where I have both arrays, the amount of characters I will use each, but the problem of this with the other is that I know the number of possible combinations of each "key." Something I do not know about the problem above.



       String A = new String"1","2","3";
      String B = new String"a","b","c";
      //key
      String AAB = new String[18];
      String ABA = new String[18];
      String BAA = new String[18];
      //result
      String S = new String[54];
      //
      //[A0,A1,B]
      int aabIndex = 0, abaIndex = 0, baaIndex=0;
      for (int a0Index = 0; a0Index < 3; a0Index++)
      for (int a1Index = 0; a1Index < 3; a1Index++)
      // skip when A0 == A1
      if (a0Index == a1Index) continue;
      // scroll through b
      for(int bIndex = 0; bIndex < 3; bIndex++)
      AAB[aabIndex++] = A[a0Index] + A[a1Index] + B[bIndex];
      ABA[abaIndex++] = A[a0Index] + B[bIndex] + A[a1Index];
      BAA[baaIndex++] = B[bIndex] + A[a0Index] + A[a1Index];





      Permutation to arrive at the above result:



      [Arrangement(3,2)*Arrangement(3,1)]*Combination(3,2)
      [(3!/(3-2)!)*(3!/(3-1)!]*[3!/(2!*(3-2)!) =
      [6 * 3] * 3 = 54


      Can anybody help me?







      java combinations permutation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:11









      J. DoeJ. Doe

      184




      184






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Try this:



          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Collections;
          import java.util.List;

          import static java.util.stream.Collectors.toList;

          public class Perm2

          public static void main(String args)
          List<String> listA = Arrays.asList("1", "2", "3");
          List<String> listB = Arrays.asList("a", "b", "c");

          List<String> result = perm2(listA, 2, listB, 1);
          result.forEach(System.out::println);
          System.out.println("--- count = " + result.size());


          private static List<String> perm2(List<String> listA, int numA, List<String> listB, int numB)
          if (numA == 0 && numB == 0) return Collections.singletonList("");

          List<String> forSelect = new ArrayList<>();
          if (numA > 0) forSelect.addAll(listA);
          if (numB > 0) forSelect.addAll(listB);

          List<String> result = new ArrayList<>();
          for (String elem : forSelect)
          List<String> newListA = without(listA, elem);
          int newNumA = numA - (listA.contains(elem) ? 1 : 0);
          List<String> newListB = without(listB, elem);
          int newNumB = numB - (listB.contains(elem) ? 1 : 0);
          result.addAll(
          perm2(newListA, newNumA, newListB, newNumB).stream()
          .map(s -> elem + s)
          .collect(toList()));

          return result;


          private static List<String> without(List<String> list, String elem)
          return list.stream().filter(e -> e != elem).collect(toList());





          I assume that all elements from listA and listB are distinct and that the numbers of elements to choose are in the range 0..length.






          share|improve this answer























          • Donat, you opened my mind with your algorithm. Thanks a lot for the help.

            – J. Doe
            Nov 15 '18 at 1:54











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306399%2fget-all-possible-combinations-of-two-array-of-string%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Try this:



          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Collections;
          import java.util.List;

          import static java.util.stream.Collectors.toList;

          public class Perm2

          public static void main(String args)
          List<String> listA = Arrays.asList("1", "2", "3");
          List<String> listB = Arrays.asList("a", "b", "c");

          List<String> result = perm2(listA, 2, listB, 1);
          result.forEach(System.out::println);
          System.out.println("--- count = " + result.size());


          private static List<String> perm2(List<String> listA, int numA, List<String> listB, int numB)
          if (numA == 0 && numB == 0) return Collections.singletonList("");

          List<String> forSelect = new ArrayList<>();
          if (numA > 0) forSelect.addAll(listA);
          if (numB > 0) forSelect.addAll(listB);

          List<String> result = new ArrayList<>();
          for (String elem : forSelect)
          List<String> newListA = without(listA, elem);
          int newNumA = numA - (listA.contains(elem) ? 1 : 0);
          List<String> newListB = without(listB, elem);
          int newNumB = numB - (listB.contains(elem) ? 1 : 0);
          result.addAll(
          perm2(newListA, newNumA, newListB, newNumB).stream()
          .map(s -> elem + s)
          .collect(toList()));

          return result;


          private static List<String> without(List<String> list, String elem)
          return list.stream().filter(e -> e != elem).collect(toList());





          I assume that all elements from listA and listB are distinct and that the numbers of elements to choose are in the range 0..length.






          share|improve this answer























          • Donat, you opened my mind with your algorithm. Thanks a lot for the help.

            – J. Doe
            Nov 15 '18 at 1:54
















          1














          Try this:



          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Collections;
          import java.util.List;

          import static java.util.stream.Collectors.toList;

          public class Perm2

          public static void main(String args)
          List<String> listA = Arrays.asList("1", "2", "3");
          List<String> listB = Arrays.asList("a", "b", "c");

          List<String> result = perm2(listA, 2, listB, 1);
          result.forEach(System.out::println);
          System.out.println("--- count = " + result.size());


          private static List<String> perm2(List<String> listA, int numA, List<String> listB, int numB)
          if (numA == 0 && numB == 0) return Collections.singletonList("");

          List<String> forSelect = new ArrayList<>();
          if (numA > 0) forSelect.addAll(listA);
          if (numB > 0) forSelect.addAll(listB);

          List<String> result = new ArrayList<>();
          for (String elem : forSelect)
          List<String> newListA = without(listA, elem);
          int newNumA = numA - (listA.contains(elem) ? 1 : 0);
          List<String> newListB = without(listB, elem);
          int newNumB = numB - (listB.contains(elem) ? 1 : 0);
          result.addAll(
          perm2(newListA, newNumA, newListB, newNumB).stream()
          .map(s -> elem + s)
          .collect(toList()));

          return result;


          private static List<String> without(List<String> list, String elem)
          return list.stream().filter(e -> e != elem).collect(toList());





          I assume that all elements from listA and listB are distinct and that the numbers of elements to choose are in the range 0..length.






          share|improve this answer























          • Donat, you opened my mind with your algorithm. Thanks a lot for the help.

            – J. Doe
            Nov 15 '18 at 1:54














          1












          1








          1







          Try this:



          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Collections;
          import java.util.List;

          import static java.util.stream.Collectors.toList;

          public class Perm2

          public static void main(String args)
          List<String> listA = Arrays.asList("1", "2", "3");
          List<String> listB = Arrays.asList("a", "b", "c");

          List<String> result = perm2(listA, 2, listB, 1);
          result.forEach(System.out::println);
          System.out.println("--- count = " + result.size());


          private static List<String> perm2(List<String> listA, int numA, List<String> listB, int numB)
          if (numA == 0 && numB == 0) return Collections.singletonList("");

          List<String> forSelect = new ArrayList<>();
          if (numA > 0) forSelect.addAll(listA);
          if (numB > 0) forSelect.addAll(listB);

          List<String> result = new ArrayList<>();
          for (String elem : forSelect)
          List<String> newListA = without(listA, elem);
          int newNumA = numA - (listA.contains(elem) ? 1 : 0);
          List<String> newListB = without(listB, elem);
          int newNumB = numB - (listB.contains(elem) ? 1 : 0);
          result.addAll(
          perm2(newListA, newNumA, newListB, newNumB).stream()
          .map(s -> elem + s)
          .collect(toList()));

          return result;


          private static List<String> without(List<String> list, String elem)
          return list.stream().filter(e -> e != elem).collect(toList());





          I assume that all elements from listA and listB are distinct and that the numbers of elements to choose are in the range 0..length.






          share|improve this answer













          Try this:



          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.Collections;
          import java.util.List;

          import static java.util.stream.Collectors.toList;

          public class Perm2

          public static void main(String args)
          List<String> listA = Arrays.asList("1", "2", "3");
          List<String> listB = Arrays.asList("a", "b", "c");

          List<String> result = perm2(listA, 2, listB, 1);
          result.forEach(System.out::println);
          System.out.println("--- count = " + result.size());


          private static List<String> perm2(List<String> listA, int numA, List<String> listB, int numB)
          if (numA == 0 && numB == 0) return Collections.singletonList("");

          List<String> forSelect = new ArrayList<>();
          if (numA > 0) forSelect.addAll(listA);
          if (numB > 0) forSelect.addAll(listB);

          List<String> result = new ArrayList<>();
          for (String elem : forSelect)
          List<String> newListA = without(listA, elem);
          int newNumA = numA - (listA.contains(elem) ? 1 : 0);
          List<String> newListB = without(listB, elem);
          int newNumB = numB - (listB.contains(elem) ? 1 : 0);
          result.addAll(
          perm2(newListA, newNumA, newListB, newNumB).stream()
          .map(s -> elem + s)
          .collect(toList()));

          return result;


          private static List<String> without(List<String> list, String elem)
          return list.stream().filter(e -> e != elem).collect(toList());





          I assume that all elements from listA and listB are distinct and that the numbers of elements to choose are in the range 0..length.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 21:53









          DonatDonat

          784127




          784127












          • Donat, you opened my mind with your algorithm. Thanks a lot for the help.

            – J. Doe
            Nov 15 '18 at 1:54


















          • Donat, you opened my mind with your algorithm. Thanks a lot for the help.

            – J. Doe
            Nov 15 '18 at 1:54

















          Donat, you opened my mind with your algorithm. Thanks a lot for the help.

          – J. Doe
          Nov 15 '18 at 1:54






          Donat, you opened my mind with your algorithm. Thanks a lot for the help.

          – J. Doe
          Nov 15 '18 at 1:54




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306399%2fget-all-possible-combinations-of-two-array-of-string%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3