forked from Lonewolf0502/DeveloperCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsequence.java
More file actions
37 lines (31 loc) · 908 Bytes
/
Subsequence.java
File metadata and controls
37 lines (31 loc) · 908 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
31
32
33
34
35
36
37
package RecursionAndBacktracking;
import java.util.ArrayList;
import java.util.Scanner;
public class stringSubsequence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.next();
ArrayList<String> res = getSubSequence(str);
System.out.println(res);
}
private static ArrayList<String> getSubSequence(String str) {
// TODO Auto-generated method stub
if(str.length() == 0) {
ArrayList<String> bres = new ArrayList<>();
bres.add(" ");
return bres;
}
char ch = str.charAt(0); // a
String ros = str.substring(1); //bc
ArrayList<String> rres = getSubSequence(ros); //[--,-c,b-,bc]
ArrayList<String> mres = new ArrayList<>();
for(String rstr: rres) {
mres.add(""+rstr);
}
for(String rstr : rres) {
mres.add(ch+rstr);
}
return mres;
}
}