Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4010134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:57:46+00:00 2026-05-20T08:57:46+00:00

I have been trying to generate a list of every possible 4 character string

  • 0

I have been trying to generate a list of every possible 4 character string which could be made up of any given set of characters. I have used a function to generate every 4 character combination from a set of characters but each character is only ever used once. I need every possible combination using a given set of chars for example:

String[] elements = {"a", "b", "c", "1", "2", "3"};
int[] indices;
CombinationGenerator x = new CombinationGenerator (elements.length, 4);
StringBuffer combination;
while (x.hasMore ()) {
  combination = new StringBuffer ();
  indices = x.getNext ();
  for (int i = 0; i < indices.length; i++) {
      combination.append (elements[indices[i]]);
  }
  System.out.println (combination.toString ());
}

Using the CombinationGenerator class from here,
this will return every unique 4 character combination such as:

'abcd' , 'abc1', 'acb2', 'acb1'

But, I want every possible string that could be created using the given characters. For example:

'aaaa', 'aaab', 'abc1', 'aac1', '11c2'

I have tried every recursive and permutation method I’ve been able to find or come up with but I’m stumped on getting any further than generating all the combinations like above, then generating every permutation of each combination, but I can’t work out how to create a set of combinations using repeated characters.

Any help, or even just the theory on how it could be done would be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:57:46+00:00Added an answer on May 20, 2026 at 8:57 am

    You’re going to have to be more specific on exactly WHAT you want your function to get. There are many different definitions of “combinations” and you haven’t specified whether you want ordered or unordered combinations.

    Mathematically, if you have n elements and want a LIST of k of them (ordered with repeats), that gives you

    n ^ k
    

    combinations. (6 ^ 4 = 1296 combinations in your original example, which is a lot!). However, if you have n elements and want a MULTISET of k of them (unordered with repeats), that gives you

    (n + k - 1)! / (k! * (n - 1)!)
    

    combinations and is a much harder enumeration.

    If k is small, you can generate the first one with a limited number of for loops but this becomes cumbersome very quickly as k grows. This strongly hints at the need for a RECURSIVE method:

    public static String[] getAllLists(String[] elements, int lengthOfList)
    {
        //initialize our returned list with the number of elements calculated above
        String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];
    
        //lists of length 1 are just the original elements
        if(lengthOfList == 1) return elements; 
        else
        {
            //the recursion--get all lists of length 3, length 2, all the way up to 1
            String[] allSublists = getAllLists(elements, lengthOfList - 1);
    
            //append the sublists to each element
            int arrayIndex = 0;
    
            for(int i = 0; i < elements.length; i++)
            {
                for(int j = 0; j < allSublists.length; j++)
                {
                    //add the newly appended combination to the list
                    allLists[arrayIndex] = elements[i] + allSublists[j];
                    arrayIndex++;
                }
            }
    
            return allLists;
        }
    }
    

    Not only will this method generate all the lists, but it will enumerate them in order. That is, the output will be

    aaaa
    aaab
    aaac
    aaa1
    aaa2
    aaa3
    aaba
    aabb
    aabc
    aab1
    ...
    3323
    333a
    333b
    333c
    3331
    3332
    3333
    

    using your original input. It can also generate any length of words (be very careful with this! Just with words of length 8 I wound up with 1,679,616 combinations!).

    If the method confuses you (it’s a recursive method, so it’s a bit hard to follow) or if you want a solution to the second combination problem, feel free to ask. Also, this method is somewhat inefficient because it recalculates the combinations for all the sublists, so it’s not viable for really long lists. If you really wanted efficiency you would store the already-calculated tuples in a global list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying real hard understanding regular expression, Is there any way I
I have been trying this for a while but thus far haven't had any
I have been trying to set up OpenCV for the past few days with
I'm trying to get the pre-processor to generate a list based on which names
Have have been trying to make a validator for my xml files. I have
I have been trying this for the whole day and I managed to make
I have been trying to get a response from a server sending a GET
I have been trying to figure out the following excerpt from Ruby on Rails
I have been trying to access an HTTPS URL with the HTMLUnitDriver API of
I have been trying to create a Main program that calls another program, sending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.