How to go about making this type of combination?

Def

I have a four letter string of letters, such as ASDF and I want to find all 3 (three) letter combinations using these letters and the three letter combination does not need to form a real word.Ex.

AAA AAS AAD AAF ADA ADS ADD ADF ............... SSA SSD SSF SSS

I am relatively new to Java, having just learned how to use the String class and using loops and conditional statements. The only way that I know how to do this is by a massive and very tedious set of for loops and if statements that account for every possibility that could arise. This would look like:

    public static void main(String[] args)
    {
      String combo = "";
      for(int counter = 1; counter <= 16; counter++){
           combo = "A";
           if(counter == 1){
              combo = combo + "AA";
           }
         // This would continue on for all the possibilities starting with "A" and
         // then move on to "S" as the lead character
       }
    }

I know that this is one of the worst ways to go about this problem, but I am really stuck as to how to do it another way. It would be easier if I had 3 letters and made the 3 letter combos, as then I could just get each letter from the array and just rearrange them, but as I'm only using 3 of the 4 letters it is more difficult. Any advice on how to get this done in an more efficient manner?

Max Truxa

Use a recursive function.

Like this (not tested, don't have a Java compiler on my laptop). Performance could probably be boosted by using StringBuilder.

static void printAllPossibilities(String charSet, int length) {
  printAllPossibilities_(charSet, length, "");
}

static void printAllPossibilities_(String charSet, int length, String temp) {
  if (length == 0) {
    System.out.println(temp);
    return;
  }
  for (int i = 0; i < charSet.length(); i++)
    printAllPossibilities_(charSet, length - 1, temp + charSet.charAt(i));
}

Usage:

printAllPossibilities("ASDF", 4); // Print all 4-letter combinations out of "ASDF"
printAllPossibilities("bar", 2); // Print all 2-letter combinations out of "bar"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to go about testing go routines?

From Dev

How to go about create and manage user?

From Dev

How do I go about making multiple instances of links in changing content work?

From Dev

How to warn about a duplicate values combination

From Dev

How to go about making an upside-down PopupMenu

From Dev

How would I go about matching like this

From Dev

How would I go about this? (Chrome extension)

From Dev

How to go about learning R-tree?

From Dev

How can I go about making a many2many relationship to self in golang gorm?

From Dev

How to go about learning java webservice

From Dev

How Would I go about making a box around the screen using Ncurses

From Dev

How do I go about making a Unix Timestamp in Milliseconds in PHP Code?

From Dev

How would I go about making three bodies of text side-by-side?

From Dev

How to go about debugging HttpURLConnection?

From Dev

How to go about making an image move up in the div on hover

From Dev

HTML- How would i go about making a section appear below another section?

From Dev

How does one go about making a hangman game where the computer shows you your progress in Python?

From Dev

How do i go about making a django model field unique but only for individual users?

From Dev

How to go about GARP?

From Dev

How to go about making a variable in a class output to a label after clicking on a picturebox?

From Dev

How would I go about creating Textbox.Text combination WYSIWYG

From Dev

How would I go about making a RewriteRule as described?

From Dev

How do I go about targeting the literal "first-child" of a div, styling depending on element type

From Dev

I'm making a GUI program in python that needs to run as root right from the get go, how should I go about it?

From Dev

How to go about debugging HttpURLConnection?

From Dev

How would I go about separating numbers in a string and making them their own values?

From Dev

How do I go about making this part of my code reactive? (Meteor + React)

From Dev

How to go about making a itemsearch bar in ionic3 and firebase

From Dev

How do I go on about making an image scroll inside a scrollview's specific area?

Related Related

  1. 1

    How to go about testing go routines?

  2. 2

    How to go about create and manage user?

  3. 3

    How do I go about making multiple instances of links in changing content work?

  4. 4

    How to warn about a duplicate values combination

  5. 5

    How to go about making an upside-down PopupMenu

  6. 6

    How would I go about matching like this

  7. 7

    How would I go about this? (Chrome extension)

  8. 8

    How to go about learning R-tree?

  9. 9

    How can I go about making a many2many relationship to self in golang gorm?

  10. 10

    How to go about learning java webservice

  11. 11

    How Would I go about making a box around the screen using Ncurses

  12. 12

    How do I go about making a Unix Timestamp in Milliseconds in PHP Code?

  13. 13

    How would I go about making three bodies of text side-by-side?

  14. 14

    How to go about debugging HttpURLConnection?

  15. 15

    How to go about making an image move up in the div on hover

  16. 16

    HTML- How would i go about making a section appear below another section?

  17. 17

    How does one go about making a hangman game where the computer shows you your progress in Python?

  18. 18

    How do i go about making a django model field unique but only for individual users?

  19. 19

    How to go about GARP?

  20. 20

    How to go about making a variable in a class output to a label after clicking on a picturebox?

  21. 21

    How would I go about creating Textbox.Text combination WYSIWYG

  22. 22

    How would I go about making a RewriteRule as described?

  23. 23

    How do I go about targeting the literal "first-child" of a div, styling depending on element type

  24. 24

    I'm making a GUI program in python that needs to run as root right from the get go, how should I go about it?

  25. 25

    How to go about debugging HttpURLConnection?

  26. 26

    How would I go about separating numbers in a string and making them their own values?

  27. 27

    How do I go about making this part of my code reactive? (Meteor + React)

  28. 28

    How to go about making a itemsearch bar in ionic3 and firebase

  29. 29

    How do I go on about making an image scroll inside a scrollview's specific area?

HotTag

Archive