-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSequence.java
More file actions
65 lines (57 loc) · 1.87 KB
/
Copy pathSubSequence.java
File metadata and controls
65 lines (57 loc) · 1.87 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package recursion.string;
import java.util.ArrayList;
import java.util.Collections;
/*
* abc, temp=""
*
*
*/
public class SubSequence {
public static void main(String[] args) {
String s = "abcd";
usingRecursion(s, 0, "");
System.out.println("\n");
usingSubStringRecursion(s, "");
System.out.println("\n");
ArrayList<String> res = AllPossibleStrings(s);
System.out.println(res);
}
private static void usingRecursion(String s, int i, String temp) {
if(i == s.length()) {
System.out.print(temp+" ");
return;
}
//extract currentChar
char currentChar = s.charAt(i);
usingRecursion(s, i+1, temp); //exclude current char
usingRecursion(s, i+1, temp + currentChar); //include current char
}
private static void usingSubStringRecursion(String s, String temp) {
if(s.isEmpty()) {
System.out.print(temp+" ");
return;
}
//extract currentChar
char currentChar = s.charAt(0); //here we always extract 0th index char
usingSubStringRecursion(s.substring(1), temp); //exclude current char
usingSubStringRecursion(s.substring(1), temp + currentChar); //include current char
}
static ArrayList<String> AllPossibleStrings(String s) {
int n = s.length();
ArrayList<String>ans=new ArrayList<>();
for (int num = 0; num < (1 << n); num++) {
StringBuilder sub = new StringBuilder();
for (int i = 0; i < n; i++) {
//check if the ith bit is set or not
if ((num & (1 << i))!=0) {
sub.append(s.charAt(i));
}
}
if (!sub.isEmpty()) {
ans.add(sub.toString());
}
}
Collections.sort(ans);
return ans;
}
}