Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(){
- String input = "abcdef";
- for(int i = 1; i < input.length(); i++)
- {
- FindSubsetsOfLength(input, i, "", i);
- }
- }
- public static void FindSubsetsOfLength(String input, int l, String prefix, int desiredLength)
- {
- // If it is possible to find a subset of length l in this prefix:
- if(input.length() > l) {
- // Case 1: Exclude the first character in input
- if(input.length() > l && l > 0){
- FindSubsetsOfLength(input.substring(1), l, prefix, desiredLength);
- }
- // Case 2: include the first character in input
- String newPrefix = prefix + input.charAt(0);
- FindSubsetsOfLength(input.substring(1), l-1, newPrefix, desiredLength);
- }
- // Otherwise, if it is not possible to find a subset of length l in this prefix:
- else if (prefix.length() == desiredLength) {
- System.out.println(prefix);
- }
- }
Add Comment
Please, Sign In to add comment