-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_11_2.java
More file actions
36 lines (30 loc) · 1.05 KB
/
Problem_11_2.java
File metadata and controls
36 lines (30 loc) · 1.05 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
package strings;
import java.util.*;
// Problem Title => Print all Subsequences of a string.
public class Problem_11_2 {
//Set to store all the subsequences
static HashSet<String> st = new HashSet<>();
static void subSequence(String str){
int n = str.length();
for(int i = 0; i < n; i++){
for(int j = n; j > i; j--){
String sub_str = str.substring(i, j);
st.add(sub_str);
// Drop kth character in the substring and if it is not in the set then recur
for (int k = 1; k < sub_str.length() - 1; k++) {
StringBuilder sb = new StringBuilder(sub_str);
// Drop character from the string
sb.deleteCharAt(k);
subSequence(sb.toString());
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();
subSequence(s);
System.out.println(st);
}
}