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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:37:35+00:00 2026-05-24T07:37:35+00:00

I was trying to find some examples on how to find a given set’s

  • 0

I was trying to find some examples on how to find a given set’s (may be string or array of integers) all combinations in Java. And I have came across this code piece (found in http://introcs.cs.princeton.edu/java/23recursion/Combinations.java.html. I have copied only the related parts in here.):

// print all subsets of the characters in s
public static void comb1(String s) { comb1("", s); }

// print all subsets of the remaining elements, with given prefix 
private static void comb1(String prefix, String s) {
    if (s.length() > 0) {
        System.out.println(prefix + s.charAt(0));
        comb1(prefix + s.charAt(0), s.substring(1));
        comb1(prefix,               s.substring(1));
    }
}  

// read in N from command line, and print all subsets among N elements
public static void main(String[] args) {
   int N = Integer.parseInt(args[0]);
   String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   String elements = alphabet.substring(0, N);

   // using first implementation
   comb1(elements);
   System.out.println();
}

But, I really do not understand how it works. Does anyone care to explain?

  • 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-24T07:37:36+00:00Added an answer on May 24, 2026 at 7:37 am

    Java programs start at main. This one takes an argument which should be an integer. It stores the integer in N. If the user typed in java and the program name with 3, then N would be set to 3. This is used to peel off the first N letters of alphabet and place them in elements. (In our example, abc). Then it calls comb1(abc), that is, the public comb1 listed first.

    Next comb1 calls the private comb1 with two arguments, an empty string and abc.

    Now the recursion begins. Private comb1 takes a prefix and a string (in the first case an empty string and abc). If the string is not empty then it:

    1. prints the first char
    2. calls itself recursively with the prefix + the first char as the new prefix and remainder as the new string and
    3. calls itself recursively with the same prefix as new prefix and all but the first character as the new string.

    (Here is where many people will tremble slightly… stare at it, hang on, be the computer, and the meaning will come.)

    (Top level)
    comb1("", "abc") -> *1* a   *2* comb1("a", "bc") *3* comb1("", "bc")
    
    (Second level)
    comb1("a", "bc") -> *1* ab  *2* comb1("ab", "c") *3* comb1("a", "c")
    comb1("", "bc")  -> *1* b   *2* comb1("b", "c")  *3* comb1("", "c")
    
    (Third level)
    comb1("ab", "c") -> *1* abc *2* comb1("abc", "") *3* comb1("ab", "")
    comb1("a", "c")  -> *1* ac  *2* comb1("a", "")   *3* comb1("a", "")
    
    comb1("b", "c")  -> *1* bc  *2* comb1("bc", "")  *3* comb1("b", "")
    comb1("", "c")   -> *1* c   *2* comb1("c", "")   *3* comb1("", "")
    
    (Fourth level)
    comb1("ab", "") -> (immediate return, ending recursion) 
    comb1("a", "") -> (immediate return, ending recursion)
    comb1("b", "") -> (immediate return, ending recursion)
    comb1("", "") -> (immediate return, ending recursion)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I'm trying to find some references in regards to x86 Assembly languages. Tutorials/Examples to
I have been trying to find some resource on this topic for a while
I have been trying to find some step by step guidance on how to
I am trying to find some examples of building a custom model binder for
Am trying to find some text only if it contains english letters and numbers
I'm trying to find some info on the best and most common RESTful url
I've been trying to find some manual information on this, but my search is
I'm trying to find some information on preferred solution setup when using MVC 2
I’m trying to find some objective comparisons of Captaris and K2 workflow products. I

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.