-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubSets.java
More file actions
34 lines (27 loc) · 859 Bytes
/
SubSets.java
File metadata and controls
34 lines (27 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.program.dynamic.programming;
/**
* Time : O(2^n)
* Number of Subsets : (2^n) - 1;
*
* C (n, k) = C(n-1, k-1) + C(n-1, k) ... with bob and without bob.
* @author rrohit
*
*/
public class SubSets {
public static void printSubset(String input){
subsets("", input);
}
private static void subsets(String output, String input) {
if (input.equals("")) {
System.out.println(output);
}else {
//add to subset and remove from input, recur
subsets(output+input.charAt(0), input.substring(1)); // with bob c (n-1, k-1)
//do not add to subset and remove from input, recur
subsets(output, input.substring(1)); // without bob C(n-1, k)
}
}
public static void main(String[] args) {
SubSets.printSubset("abcd");
}
}