d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Java -- Recursion!
Add Reply New Topic New Poll
Member
Posts: 2,020
Joined: Apr 30 2011
Gold: 143.00
Dec 5 2012 08:09pm
Code
import java.util.Collections;
import java.util.ArrayList;

/**
  This program tests the subset generator.
*/
public class SubsetGeneratorTester
{
  public static void main(String[] args)
  {
     SubsetGenerator generator
           = new SubsetGenerator("rum");
     
     ArrayList<String> subsets = generator.getSubsets();
     // Sort the result for checking
     Collections.sort(subsets);
     System.out.println(subsets);
     System.out.println("Expected: [, m, r, rm, ru, rum, u, um]");
  }
}


And

Code
import java.util.ArrayList;

/**
  This class generates subsets of a string.
*/
public class SubsetGenerator
{
  /**
     Constructs a subset generator.
     @param aWord the word to obtain subsets from
  */
  ....

  public ArrayList<String> getSubsets()
  {
     ....

     // Form a simpler word by removing the first character
     ....

     // Generate all subsets of the simpler word
     ....

     // Add the removed character to the front of
     // each subset of the simpler word, and
     // also include the word without the removed character
     ....
     
     // Return all subsets
     ....
  }

  ....
}


Is my starter code. I sort of worked around it and have this:


Code
import java.util.Collections;
import java.util.ArrayList;

/**
  This program tests the subset generator.
*/
public class SubsetGeneratorTester
{
  public static void main(String[] args)
  {
     SubsetGenerator generator
           = new SubsetGenerator("rum");
     
     String subsets = generator.nextGen();
     // Sort the result for checking
     System.out.println(subsets);
     System.out.println("Expected: [, m, r, rm, ru, rum, u, um]");
  }
}


Code
import java.util.*;
import java.io.*;
 
public class SubsetGenerator
{
 
    private String subset;
    private int current;
    private SubsetGenerator tailGenerator;
    boolean addedFirst;
 
    public SubsetGenerator(String aSubset)
    {        
     subset = aSubset;
     addedFirst = subset.length() == 0;
        if (subset.length() > 0)
           tailGenerator = new SubsetGenerator(subset.substring(1));
    }
   
    public String nextGen()
    {
        if (subset.length() == 1)
        {
            current++;
            return subset;
        }
 
        String r = subset.charAt(current) + tailGenerator.nextGen();
       
        if (!tailGenerator.SubsetGen())
        {
            current++;
            if (current < subset.length())
            {
                String tailString = subset.substring(0, current)
                       + subset.substring(current + 1);
                tailGenerator = new SubsetGenerator(tailString);
            }
            return r;
        }
       
        return "none";
    }
 
    public boolean SubsetGen()
    {
        return addedFirst || tailGenerator != null && tailGenerator.SubsetGen();
    }
 
}


This doesnt work either. Can anyone help? Heres the actual question:

Implement a SubsetGenerator that generates all subsets of the characters of a string. For example, the subsets of the characters of all string "rum" are the eight strings
"rum","ru","rm","r","um","u","m", and ""
Note that the subsets don't have to be substrings -- for example, "rm" isn't a substring of "rum".


Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 5 2012 08:21pm
Didn't read your code.

should be pretty straight forward since you only want substrings.

findsubstrings(String value){
print(value)
if value is empty stop
findsubstrings(value without left char)
findsubstrings(value without right char)
}

you'll need extra logic to remove duplicates. maybe an external Set to hold everything

/edit: oops misread it. thought you wanted substrings and not subsets. oh well. google around for a power set implementation in java.

This post was edited by carteblanche on Dec 5 2012 08:34pm
Member
Posts: 2,020
Joined: Apr 30 2011
Gold: 143.00
Dec 5 2012 09:36pm
^ bump
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Dec 6 2012 01:19am
CBA to write this in Java, but here's the Python version. Logic should be identical. You have most of it anyway - it's just a matter of putting together the pieces in a sensible fashion.

Code
def subsets_helper(input_str, lst):
 if len(input_str) == 0:
   return lst

 if input_str not in lst:
   lst.append(input_str)
 for i in range(len(input_str)):
   subsets_helper(input_str[0:i] + input_str[i+1:len(input_str)], lst)
 return lst

def subsets(input_str):
 return subsets_helper(input_str, [''])


print h('rum')


This post was edited by irimi on Dec 6 2012 01:25am
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Dec 10 2012 12:22pm
Code
   public class SubsetGenerator {

       private String text;

       public SubsetGenerator(String text) {
           this.text = text;
       }

       public ArrayList<String> getSubsets() {
           return getSubsets(text);
       }

       private ArrayList<String> getSubsets(String word) {
           ArrayList<String> subsets = new ArrayList<String>();

           if (word.equals("")) {
               subsets.add("");
               return subsets;
           }

           String firstLetter = word.substring(0, 1);
           String simplerWord = word.substring(1);

           for (String simplerSubset : getSubsets(simplerWord)) {
               subsets.add(firstLetter + simplerSubset);
               subsets.add(simplerSubset);
           }

           return subsets;
       }
   }
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll