-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg_subseq_rec.java
More file actions
30 lines (27 loc) · 892 Bytes
/
g_subseq_rec.java
File metadata and controls
30 lines (27 loc) · 892 Bytes
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
import java.util.ArrayList;
public class g_subseq_rec {
static ArrayList<String> solution(String str) {
if(str.length() == 0) {
ArrayList<String> arr = new ArrayList<>();
arr.add("");
return arr;
}
// str = "ravi"
// currentChar = 'r'
char currentChar = str.charAt(0);
// remainingString = "avi"
String remainingString = str.substring(1);
ArrayList<String> temp = solution(remainingString);
ArrayList<String> result = new ArrayList<>();
for(String s : temp) {
result.add(s);
result.add(currentChar + s);
}
return result;
}
public static void main(String[] args) {
String str = "ravi";
ArrayList<String> output = solution(str);
System.out.println(output);
}
}