I want to find all subsets of a given set.
I got string set defined as following: HashSet<String> L
and I want to use in a loop all of its subsets as: for each do something.
Is there an easy way with low complexity to do that?
OK, I used this algorithm (L
is a set of strings):
powerSet = new HashSet<List<String>>();
List<String> mainList = new ArrayList<String>(L);
buildPowerSet(mainList,mainList.size());
And,
private static void buildPowerSet(List<String> list, int count)
{
powerSet.add(list);
for(int i=0; i<list.size(); i++)
{
List<String> temp = new ArrayList<String>(list);
temp.remove(i);
buildPowerSet(temp, temp.size());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With