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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:14:06+00:00 2026-05-11T20:14:06+00:00

Edited after getting answers Some excellent answers here. I like Josh’s because it is

  • 0

Edited after getting answers

Some excellent answers here. I like Josh’s because it is so clever and uses C++. However I decided to accept Dave’s answer because of it’s simplicity and recursion. I tested them both and they both produced identical correct results (although in a different order). So thanks again everyone.


Say I have a string s of chars s[0]:s[N] and where each char s[i] <= s[i+1]
For example the string

aaacdddghzz

I want to generate all combinations of substrings while keeping the same relationship between chars.

So for example I would get

a
aa
aaa
ad
aad
aaad
add
aadd
aaadd
addd
aaddd
aaaddd
d
dd
ddd
.
.
.
ac
aac
.
.
.
acdddghzz
aacdddghzz
aaacdddghzz

But not

ca
hdz
...etc

Now I know how to work out how many combinations there are. You create a histogram of the frequency of letters in the string. So in the above example the that would be

For string aaacdddghzz

a=3
d=3
c=1
g=1
h=1
z=2

and the formula is (a+1)(c+1)(d+1)(g+1)(h+1)(z+1) = 4*4*2*2*2*3 = 384. There are 384 substrings that keep the s[i] <=s [i+1] relationship.

So the question is how do I generate those 384 substrings recursively? Actually an iterative method would be just as good, maybe better as large strings with many unique chars might cause the stack to overflow. This sounds like homework but it isn’t. I’m just useless at coming up with algorithms like this. I use C++ but pseudocode would be fine.

  • 1 1 Answer
  • 1 View
  • 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-11T20:14:06+00:00Added an answer on May 11, 2026 at 8:14 pm

    Following is a recursive algorithm to generate all subsequences.

    /* in C -- I hope it will be intelligible */
    
    #include <stdio.h>
    
    static char input[] = "aaabbbccc";
    static char output[sizeof input];
    
    /* i is the current index in the input string
     * j is the current index in the output string
     */
    static void printsubs(int i, int j) {
        /* print the current output string */
        output[j] = '\0';
        printf("%s\n", output);
        /* extend the output by each character from each remaining group and call ourselves recursively */
        while(input[i] != '\0') {
            output[j] = input[i];
            printsubs(i + 1, j + 1);
            /* find the next group of characters */
            do ++i;
            while(input[i] == input[i - 1]);
        }
    }
    
    int main(void) {
        printsubs(0, 0);
        return 0;
    }
    

    If your interest is merely in counting how many subsequences there are, you can do it much more efficiently. Simply count up how many of each letter there are, add 1 to each value, and multiply them together. In the above example, there are 3 a’s, 3 b’s, 3 c’s, and 2 d’s, for (3 + 1) * (3 + 1) * (3 + 1) * (2 + 1) = 192 subsequences. The reason this works is that you can choose between 0 and 3 a’s, 0 and 3 b’s, 0 and 3 c’s, and 0 and 2 d’s, and all of these choices are independent.

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

Sidebar

Related Questions

After getting some url parameters, I would like to use them later in my
[Edited: After cross-testing on a fresh machine and some additional research, this appears to
Edited in accordance with comments For some reason, I seem to be getting this
I edited this question after i found a solution... i need to understand why
I have this test page [edited by poster after question was answered] and something
I'm getting some very slow performance from an Umbraco site hosted on my Azure
I am having an issue with getting mod_rewrite rules working in .htaccess after moving
Edited : I've changed the code as follows and getting an 'NaN' error! Can
I had some problems after starting a new coredata project with the xcode 3.2.5...
I'm getting a firebug error: missing : after property id error source line: if(jQuery.inArray(mmDialogButton.CANCEL,

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.