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

  • Home
  • SEARCH
  • 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 6586255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:46:05+00:00 2026-05-25T16:46:05+00:00

Given the following recursive Java method: protected static void makeStringsList(List<String> strings, List<Character> letters) {

  • 0

Given the following recursive Java method:

protected static void makeStringsList(List<String> strings, List<Character> letters) {
  if (letters.size() == 2) {
    strings.add(letters.get(0) + "" + letters.get(1));
    strings.add(letters.get(1) + "" + letters.get(0));
  }
  else {
    Character c = letters.remove(0);
    makeStringsList(strings, letters);
    List<String> tempList = new ArrayList<String>();
    for (String s : strings) {
      StringBuffer buffer = new StringBuffer(s);
      for (int index = 0; index < s.length() + 1; index++) {
        buffer.insert(index, c);
        tempList.add(buffer.toString());
        buffer = new StringBuffer(s);
      }
    }
    strings.addAll(tempList);
  }
}

Given N letters, the code above generates a list that contains permutations of strings that use all of those same N letters. For example, given (1, 2, 3), it generates:

  • 123
  • 132
  • 213
  • 231
  • 312
  • 321

It also adds strings with fewer than N letters to the list, but they’re not exhaustive. Using the same example, 23 and 32 are also in the list, but 12, 21, 13, and 31 are not in the list.

I had originally created the method above for a kata I was previously working on here during my free time, and I want to modify it now so that I can make it more general purpose and return a list containing permutations of strings with lengths 2 to N given N letters. Is it possible to modify the method above to accomplish this task? Any tips? Your advice will be greatly appreciated!

  • 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-25T16:46:05+00:00Added an answer on May 25, 2026 at 4:46 pm

    I would like to help out with this problem.

    I think the best way to go about this is to generate the power set of the set of characters given, and then find the set of combinations of strings that each set in the power set can generate.

    List <String> allPermutationsOfSubsequences( Set <Character> chars ) {
    
        Set < Set <Character> > powerSetOfChars = generatePowerSet ( chars );
    
        List <String> permutations = new ArrayList <String> ();
    
        for (Set <Character> subsequence : powerSetOfChars)
            permutations.addAll( generatePermutations ( subsequence ) );
    
        return permutations;
    }
    
    Set <Set <Character>> generatePowerSet ( Set <Character> set ) {
        Set < Set <Character> > powerSet = new HashSet < Set <Character> > ();
        if (set.size() == 0) {
            powerSet.add(new HashSet <Character> ());
            return powerSet;
        }
    
        Character anElement = set.iterator().next();
        set.remove(anElement);
    
        for (Set <Character> subset : powerSet(set)) {
            Set <Character> setWithElement = new HashSet <Character> ();
            setWithElement.add(anElement);
            setWithElement.addAll(subset);
            powerSet.add(newSet);
            powerSet.add(subset);
        }
    
        set.add(anElement);
    
        return powerSets;
    }
    
    //Generates a list of permutations of the characters provided in the set.
    List <String> generatePermutations ( Set <Character> chars );
    

    The generatePowerSet method creates all the sets, so it also includes sets of size 0 and 1. You can remove those if you like, but the main idea is there.

    Sample output: [3, 2, 1, 31, 13, 32, 23, 21, 12, 321, 312, 231, 213, 132, 123]

    All you need to do is remove the ones of size 1.

    For the full code, that has been compiled and has shown to work, just go here and try it yourself!

    http://pastebin.com/P3YMmT2m

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

Sidebar

Related Questions

Given the following recursive function, what would be printed by mysterious(4)? void mysterious(int x)
Given the following recursive query: WITH DepartmentHierarchy (DepartmentID, Name, IsInactive, IsSpecial, ParentId, HierarchyLevel) AS
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int
Given following Ruby statements: (Read input and store each word in array removing spaces
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following: declare @a table ( pkid int, value int ) declare @b
Given the following XML structure <html> <body> <div> <span>Test: Text2</span> </div> <div> <span>Test: Text3</span>

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.