fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class PrintPowerSet {
  9. private static String getSubset(String inputSet, int mask) {
  10. if (mask == 0) {
  11. return "Ø";
  12. }
  13.  
  14. StringBuffer subset = new StringBuffer();
  15.  
  16. for (int i = 0; i < inputSet.length(); i++) {
  17. // Check if ith bit is set
  18. if ((mask & (1 << i)) != 0) {
  19. subset.append(inputSet.charAt(i));
  20. }
  21. }
  22.  
  23. return subset.toString();
  24. }
  25.  
  26. public static List<String> permuteSet(String inputSet) {
  27. List<String> powerSet = new ArrayList<String>();
  28. int n = inputSet.length(); // size for binary
  29. int powerSetLength = 1 << n; // you should check that n is small enough
  30.  
  31. for (int i = 0; i < powerSetLength; i++) {
  32. powerSet.add(getSubset(inputSet, i));
  33. }
  34.  
  35. return powerSet;
  36. }
  37.  
  38. public static void main(String args[]) {
  39. String set = "gle";
  40. List<String> result = permuteSet(set);
  41. for (String word : result) {
  42. System.out.print(word + " ");
  43. }
  44. }
  45. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
Ø g l gl e ge le gle