-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSequence.java
More file actions
77 lines (60 loc) · 2.15 KB
/
Copy pathSubSequence.java
File metadata and controls
77 lines (60 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
package Recursion.String;
import java.util.ArrayList;
public class SubSequence {
public static void main(String[] args) {
subseq("", "abc");
System.out.println(subseqArray("", "abc"));
subseqAscii("", "abc");
System.out.println(subseqArrayAscii("", "abc"));
}
static void subseq(String p, String up){
if (up.isEmpty()){
System.out.println(p);
return;
}
char ch = up.charAt(0);
subseq(p + ch, up.substring(1));
subseq(p, up.substring(1));
}
// if a return type is array.
static ArrayList<String> subseqArray(String p, String up){
if (up.isEmpty()){ // Base condition.
// create a new arraylist and add all processed values in it.
ArrayList<String> list = new ArrayList<>();
list.add(p);
return list;
}
char ch = up.charAt(0);
ArrayList<String> left = subseqArray(p + ch, up.substring(1));
ArrayList<String> right = subseqArray(p, up.substring(1));
left.addAll(right);
return left;
}
// subsequence with ascii value
static void subseqAscii(String p, String up){
if (up.isEmpty()){
System.out.println(p);
return;
}
char ch = up.charAt(0);
subseqAscii(p + ch, up.substring(1));
subseqAscii(p, up.substring(1));
subseqAscii(p + (ch + 0), up.substring(1));
}
static ArrayList<String> subseqArrayAscii(String p, String up){
if (up.isEmpty()){ // Base condition.
// create a new arraylist and add all processed values in it.
ArrayList<String> list = new ArrayList<>();
list.add(p);
return list;
}
char ch = up.charAt(0);
ArrayList<String> first = subseqArrayAscii(p + ch, up.substring(1));
ArrayList<String> second = subseqArrayAscii(p, up.substring(1));
ArrayList<String> third = subseqArrayAscii(p + (ch +0), up.substring(1));
first.addAll(second);
first.addAll(third);
return first;
}
}
// You can ignore the edge cases to ignore the empty ones.