-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_Subsequence_of_String.java
38 lines (33 loc) · 1.1 KB
/
Get_Subsequence_of_String.java
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
package Hard;
import java.util.ArrayList;
import java.util.Scanner;
public class Get_Subsequence_of_String {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();// abc
ArrayList<String> list = subSequence(str);
System.out.println(list);
}
//Method 1:
//Faith: bc -> b-, --, bc, -c
//Expectation: abc -> -b-, ---, -bc, --c, ab-, a--, abc, a-c
private static ArrayList<String> subSequence(String str) {
if(str.length() == 0){
ArrayList<String> base = new ArrayList<>();
base.add("");
return base;
}
char firstCharacter = str.charAt(0);//a
String restString = str.substring(1);//bc
ArrayList<String> resultRecursion = subSequence(restString);
ArrayList<String> finalAnswer = new ArrayList<>();
for (String e: resultRecursion) {
finalAnswer.add(""+e);
}
for (String e: resultRecursion) {
finalAnswer.add(firstCharacter+e);
}
return finalAnswer;
}
//Method 2:
}