-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_10.java
More file actions
43 lines (34 loc) · 1.24 KB
/
Problem_10.java
File metadata and controls
43 lines (34 loc) · 1.24 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_10 {
// str : Stores input string
// n : Length of str.
// currentPermutation : Stores current Permutation
// index : Index in current Permutation
static void printSubSeqRec(String str, int n, int idx, String currentPermutation) {
if (idx == n)
return;
if (currentPermutation != null && !currentPermutation.trim().isEmpty())
System.out.println(currentPermutation);
for (int i = idx + 1; i < n; i++) {
currentPermutation += str.charAt(i);
printSubSeqRec(str, n, i, currentPermutation);
// backtracking
currentPermutation = currentPermutation.substring(0, currentPermutation.length() - 1);
}
}
// Generates power set in lexicographic order.
static void printSubSeq(String str) {
int index = -1;
String currentPermutation = "";
printSubSeqRec(str, str.length(), index, currentPermutation);
}
// Driver Code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
printSubSeq(str);
sc.close();
}
}