Guest User

Untitled

a guest
Dec 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1.     public static void main(){
  2.         String input = "abcdef";
  3.         for(int i = 1; i < input.length(); i++)
  4.         {
  5.             FindSubsetsOfLength(input, i, "", i);
  6.         }
  7.     }
  8.  
  9.     public static void FindSubsetsOfLength(String input, int l, String prefix, int desiredLength)
  10.     {
  11.         // If it is possible to find a subset of length l in this prefix:
  12.         if(input.length() > l) {
  13.  
  14.             // Case 1: Exclude the first character in input
  15.             if(input.length() > l && l > 0){
  16.                 FindSubsetsOfLength(input.substring(1), l, prefix, desiredLength);
  17.             }
  18.  
  19.             // Case 2: include the first character in input
  20.             String newPrefix = prefix + input.charAt(0);
  21.             FindSubsetsOfLength(input.substring(1), l-1, newPrefix, desiredLength);
  22.         }
  23.         // Otherwise, if it is not possible to find a subset of length l in this prefix:
  24.         else if (prefix.length() == desiredLength) {
  25.             System.out.println(prefix);
  26.         }
  27.  
  28.     }
Add Comment
Please, Sign In to add comment