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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:19:32+00:00 2026-05-25T06:19:32+00:00

I’m trying to write a recursive function that gets all permutations with repetitions of

  • 0

I’m trying to write a recursive function that gets all permutations with repetitions of a given list.

Eg. set = ABC
1. AAA
2. AAB
3. AAC
4. ABA 
N. CCC

I want a recursive version of this code so I can get permutations for sets of any size:

for i=0; i<S.size(); i++ {
   for j=0; j<S.size(); j++ {
      for k=0; k<S.size(); k++ {

         perm[0] = S[i];
         perm[1] = S[j];
         perm[2] = S[k];
         permutations.push(combo);

      }
   }
}

I’m having some trouble wrapping my head around the problem. So far I’m thinking I need to find when I’ve reached an arbitrary depth to stop re-cursing.

Edit: I’d prefer a pseudo-code solution, I’m not implementing this in C++

  • 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-25T06:19:32+00:00Added an answer on May 25, 2026 at 6:19 am

    I think an iterative solution will be more efficient, and it can be written to support arbitrary dimensions and numbers of symbols. The code is in C++, but I delibaretely kept it simple so that you can easily translate into pseudocode or other language:

    #include <vector>
    #include <cassert>
    #include <iostream>
    
    void generate_combinations(const std::vector<char>& symbols, const unsigned int dimension, std::vector<std::vector<char> >& output)
    {
        assert( symbols.size() ); // terminate the program if condition not met
        std::vector<char> current_output(dimension);
        std::vector<unsigned int> current_combo(dimension + 1, 0);
        const unsigned int max_symbol_idx = symbols.size() - 1;
        size_t current_index = 0;
        while (current_combo.back() == 0) {
            // add current combination
            for (unsigned int i = 0; i < dimension; ++i) {
                current_output[i] = symbols[current_combo[i]];
            }
            output.push_back(current_output);
    
            // move to next combination
            while (current_index <= dimension && current_combo[current_index] == max_symbol_idx) {
                current_combo[current_index] = 0;
                ++current_index;
            }
            if (current_index <= dimension) {
                ++current_combo[current_index];
            }
            current_index = 0;
        }
    }
    
    int main()
    {
        const unsigned int dimension = 3;
        std::vector<char> symbols(4);   
        symbols[0] = 'A';
        symbols[1] = 'B';
        symbols[2] = 'C';
        symbols[3] = 'D';
        std::vector<std::vector<char> > output;
        generate_combinations(symbols, dimension, output);
        for (unsigned int i = 0; i < output.size(); ++i) {
            for (unsigned int j = 0; j < dimension; ++j) {
                std::cout << output[i][j]; // write symbol to standard output
            }
            std::cout << std::endl; // write new line character
        }
    }
    

    The output should be:

    AAA BAA CAA DAA ABA BBA CBA DBA ACA BCA CCA DCA ADA BDA CDA DDA AAB
    BAB CAB DAB ABB BBB CBB DBB ACB BCB CCB DCB ADB BDB CDB DDB AAC BAC
    CAC DAC ABC BBC CBC DBC ACC BCC CCC DCC ADC BDC CDC DDC AAD BAD CAD
    DAD ABD BBD CBD DBD ACD BCD CCD DCD ADD BDD CDD DDD

    If you want the symbols in the last position to change fastest, just reverse the contents of each row of the generated output.

    Of course, you can make generate_combinations a template function and make it work with other types than char.

    ============ UPDATE =================

    A recursive solution is, of course, more elegant:

    void add_next_symbol(const std::vector<char>& symbols, const unsigned int dimension, std::vector<char>& current_output, std::vector<std::vector<char> >& output)
    {
        if (dimension == 0) {
            output.push_back(current_output);
        } else {
            for (unsigned int i = 0; i < symbols.size(); ++i) {
                current_output.push_back(symbols[i]);
                add_next_symbol(symbols, dimension - 1, current_output, output);
                current_output.pop_back();
            }
        }
    }
    
    void generate_combinations_recursive(const std::vector<char>& symbols, const unsigned int dimension, std::vector<std::vector<char> >& output)
    {
        std::vector<char> current_output;
        add_next_symbol(symbols, dimension, current_output, output);
    }
    

    Use it in place of generate_combinations function in the first program. It should give you the same output as before.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.