-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintAllSubsequences.java
More file actions
37 lines (33 loc) · 1.17 KB
/
PrintAllSubsequences.java
File metadata and controls
37 lines (33 loc) · 1.17 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
package String;
import java.util.*;
public class PrintAllSubsequences {
public static void printSubSequence(String s, int i, StringBuilder sb) {
if(i == s.length()) System.out.print(sb + " ");
else {
//System.out.println(i + " " + sb);
printSubSequence(s,i+1, new StringBuilder(sb).append(s.charAt(i)));
//System.out.println("2 --> " + i + " " + sb);
printSubSequence(s,i+1,new StringBuilder(sb));
}
}
public static void printAllSubSequence(String s, int i, StringBuilder sb) {
if(i == s.length()) System.out.print(sb + " ");
else {
sb.append(s.charAt(i));
printAllSubSequence(s,i+1,sb);
sb.deleteCharAt(sb.length()-1);
printAllSubSequence(s,i+1,sb);
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
//int n = sc.nextInt();
String s = sc.next();
//printSubSequence(s,0,new StringBuilder());
printAllSubSequence(s,0,new StringBuilder());
System.out.println();
}
}
}