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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:25:32+00:00 2026-06-01T18:25:32+00:00

I’m trying to resolve all the combinations of elements based on a given string.

  • 0

I’m trying to resolve all the combinations of elements based on a given string.

The string is like this :

String result="1,2,3,###4,5,###6,###7,8,";

The number of element between ### (separated with ,) is not determined and the number of “list” (part separated with ###) is not determined either.

NB : I use number in this example but it can be String too.

And the expected result in this case is a string containing :

String result = "1467, 1468, 1567, 1568, 2467, 2468, 2567, 2568, 3467, 3468, 3567, 3568"

So as you can see the elements in result must start with an element of the first list then the second element must be an element of the second list etc…

From now I made this algorithm that works but it’s slow :

    String [] parts = result.split("###");
    if(parts.length>1){
        result="";
        String stack="";
        int i;
        String [] elmts2=null;

        String [] elmts = parts[0].split(",");
        for(String elmt : elmts){               //Browse root elements
            if(elmt.trim().isEmpty())continue;

            /**
             * This array is used to store the next index to use for each row.
             */
            int [] elmtIdxInPart= new int[parts.length];

            //Loop until the root element index change.
            while(elmtIdxInPart[0]==0){

                stack=elmt;

                //Add to the stack an element of each row, chosen by index (elmtIdxInPart)
                for(i=1 ; i<parts.length;i++){
                    if(parts[i].trim().isEmpty() || parts[i].trim().equals(","))continue;
                    String part = parts[i];
                    elmts2 = part.split(",");
                    stack+=elmts2[elmtIdxInPart[i]];
                }
                //rollback i to previous used index
                i--;

                if(elmts2 == null){
                    elmtIdxInPart[0]=elmtIdxInPart[0]+1;
                }
                //Check if all elements in the row have been used.
                else if(elmtIdxInPart[i]+1 >=elmts2.length || elmts2[elmtIdxInPart[i]+1].isEmpty()){

                    //Make evolve previous row that still have unused index
                    int j=1;
                    while(elmtIdxInPart[i-j]+1 >=parts[i-j].split(",").length || 
                            parts[i-j].split(",")[elmtIdxInPart[i-j]+1].isEmpty()){
                        if(j+1>i)break;
                        j++;
                    }
                    int next = elmtIdxInPart[i-j]+1;
                    //Init the next row to 0.
                    for(int k = (i-j)+1 ; k <elmtIdxInPart.length ; k++){
                        elmtIdxInPart[k]=0;
                    }
                    elmtIdxInPart[i-j]=next;
                }
                else{
                    //Make evolve index in current row, init the next row to 0.
                    int next = elmtIdxInPart[i]+1;
                    for(int k = (i+1) ; k <elmtIdxInPart.length ; k++){
                        elmtIdxInPart[k]=0;
                    }
                    elmtIdxInPart[i]=next;
                }
                //Store full stack
                result+=stack+",";
            }
        }
    }
    else{
        result=parts[0];
    }

I’m looking for a more performant algorithm if it’s possible. I made it from scratch without thinking about any mathematical algorithm. So I think I made a tricky/slow algo and it can be improved.

Thanks for your suggestions and thanks for trying to understand what I’ve done 🙂

EDIT

Using Svinja proposition it divide execution time by 2:

        StringBuilder res = new StringBuilder();
        String input = "1,2,3,###4,5,###6,###7,8,";
        String[] lists = input.split("###");
        int N = lists.length;
        int[] length = new int[N];
        int[] indices = new int[N];
        String[][] element = new String[N][];
        for (int i = 0; i < N; i++){
            element[i] = lists[i].split(",");
            length[i] = element[i].length;
        }

        // solve
        while (true)
        {
            // output current element
            for (int i = 0; i < N; i++){
                res.append(element[i][indices[i]]);
            }
            res.append(",");

            // calculate next element
            int ind = N - 1;
            for (; ind >= 0; ind--)
                if (indices[ind] < length[ind] - 1) break;
            if (ind == -1) break;

            indices[ind]++;
            for (ind++; ind < N; ind++) indices[ind] = 0;
        }
        System.out.println(res);
  • 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-06-01T18:25:33+00:00Added an answer on June 1, 2026 at 6:25 pm

    This is my solution. It’s in C# but you should be able to understand it (the important part is the “calculate next element” section):

        static void Main(string[] args)
        {
            // parse the input, this can probably be done more efficiently
            string input = "1,2,3,###4,5,###6,###7,8,";
            string[] lists = input.Replace("###", "#").Split('#');
            int N = lists.Length;
            int[] length = new int[N];
            int[] indices = new int[N];
            for (int i = 0; i < N; i++)
                length[i] = lists[i].Split(',').Length - 1;
    
            string[][] element = new string[N][];
            for (int i = 0; i < N; i++)
            {
                string[] list = lists[i].Split(',');
                element[i] = new string[length[i]];
                for (int j = 0; j < length[i]; j++)
                    element[i][j] = list[j];
            }
    
            // solve
            while (true)
            {
                // output current element
                for (int i = 0; i < N; i++) Console.Write(element[i][indices[i]]);
                Console.WriteLine(" ");
    
                // calculate next element
                int ind = N - 1;
                for (; ind >= 0; ind--)
                    if (indices[ind] < length[ind] - 1) break;
                if (ind == -1) break;
    
                indices[ind]++;
                for (ind++; ind < N; ind++) indices[ind] = 0;
            }
        }
    

    Seems kind of similar to your solution. Does this really have bad performance? Seems to me that this is clearly optimal, as the complexity is linear with the size of the output, which is always optimal.

    edit: by “similar” I mean that you also seem to do the counting with indexes thing. Your code is too complicated for me to go into after work. 😀

    My index adjustment works very simply: starting from the right, find the first index we can increase without overflowing, increase it by one, and set all the indexes to its right (if any) to 0. It’s basically counting in a number system where each digit is in a different base. Once we can’t even increase the first index any more (which means we can’t increase any, as we started checking from the right), we’re done.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.