java字符串的子集_用递归 求出一个string的所有子集 java

cat====cat,ca,ct,at,c,a,t,还有个空集publicArrayListgetSubsets(){ArrayListsubsets=newArrayList();if(word.length()==0){subsets.add(word);returnsubse...

cat ==== cat, ca, ct, at, c, a, t, 还有个空集

public ArrayList getSubsets()

{

ArrayList subsets = new ArrayList();

if(word.length() == 0)

{

subsets.add(word);

return subsets;

}

for(int i = 0; i < word.length(); i++)

{

String firstWord = word.substring(0, 1);

String remainWord = word.substring(1);

subsets.add(firstWord);

SubsetGenerator remainSubsetGenerator = new SubsetGenerator(remainWord);

ArrayList remainWordSubsets = remainSubsetGenerator.getSubsets();

for(String s : remainWordSubsets)

{

subsets.add(firstWord + s);

}

}

return subsets;

}

这是我现在写的得出[r, r, r, ru, ru, ru, ru, ru, ru, rum, rum, rum, rum, rum, rum, rum, rum, rum, rum, rum, rum]

Expected: [, m, r, rm, ru, rum, u, um]

展开

你可能感兴趣的:(java字符串的子集)