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 7646075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:06:48+00:00 2026-05-31T10:06:48+00:00

Having problems trying to show every combination of a character of array without repeating

  • 0

Having problems trying to show every combination of a character of array without repeating letters.

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;
    }
}

The above code works perfect but use’s each letter more than once which cant be done in this case.

And i am stuck how to do this now.

  • 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-31T10:06:50+00:00Added an answer on May 31, 2026 at 10:06 am

    Here’s an example implementation. Essentially it takes a String and iterates over every character, putting that character at the front. It then recurses on the remaining characters. That structure removes your issue of repeated letters, because the input to the recursion has removed the character you’ve already used.

    I also stored results in a set to remove semantic equivalences. The input ‘aab’ can switch char 0 and char 1 but still be ‘aab.’ I used a TreeSet to preserve ordering for easier verification of the output, but HashSet would be faster.

      public static Set<String> permute(String chars)
      {
        // Use sets to eliminate semantic duplicates (aab is still aab even if you switch the two 'a's)
        // Switch to HashSet for better performance
        Set<String> set = new TreeSet<String>();
    
        // Termination condition: only 1 permutation for a string of length 1
        if (chars.length() == 1)
        {
          set.add(chars);
        }
        else
        {
          // Give each character a chance to be the first in the permuted string
          for (int i=0; i<chars.length(); i++)
          {
            // Remove the character at index i from the string
            String pre = chars.substring(0, i);
            String post = chars.substring(i+1);
            String remaining = pre+post;
    
            // Recurse to find all the permutations of the remaining chars
            for (String permutation : permute(remaining))
            {
              // Concatenate the first character with the permutations of the remaining chars
              set.add(chars.charAt(i) + permutation);
            }
          }
        }
        return set;
      }
    

    Example run:

      public static void main(String[] args)
      {
        for (String s : CharPermuter.permute("abca"))
        {
          System.out.println(s);
        }
      }
    

    Generates:

    aabc
    aacb
    abac
    abca
    acab
    acba
    baac
    baca
    bcaa
    caab
    caba
    cbaa
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems trying to adjust the width of a column of a datagrid.
I have been having some problems trying to get my PHP running. When I
I'm having some problems trying to get the code below to output the data
I'm having some problems trying to perform a query. I have two tables, one
Having problems with a small awk script, Im trying to choose the newest of
I've been having a lot of problems trying to figure out how to use
Just trying out Hibernate (with Annotations) and I'm having problems with my mappings. I
I am having problems connecting to a Sqlite database through System.Data.Sqlite. I was trying
I'm trying to host the CLR inside my C++ application and I'm having problems
I am having problems with my JScript code. I am trying to loop through

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.