-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_11_3.java
More file actions
43 lines (35 loc) · 1.11 KB
/
Problem_11_3.java
File metadata and controls
43 lines (35 loc) · 1.11 KB
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
35
36
37
38
39
40
41
42
43
package strings;
import java.util.*;
// Problem Title => Print all Subsequences of a string.
public class Problem_11_3 {
// str : Stores input string
// n : Length of str.
// curr : Stores current permutation
// index : Index in current permutation, curr
static void printSubSeqRec(String str, int n, int index, String curr) {
// base case
if (index == n)
return;
if (curr != null && !curr.trim().isEmpty())
System.out.println(curr);
for (int i = index + 1; i < n; i++) {
curr += str.charAt(i);
printSubSeqRec(str, n, i, curr);
// backtracking
curr = curr.substring(0, curr.length() - 1);
}
}
// Generates power set in lexicographic order.
static void printSubSeq(String str) {
int index = -1;
String curr = "";
printSubSeqRec(str, str.length(), index, curr);
}
// Driver code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
printSubSeq(str);
}
}