-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.java
More file actions
78 lines (61 loc) · 2.68 KB
/
Copy pathStream.java
File metadata and controls
78 lines (61 loc) · 2.68 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
78
package Recursion.String;
public class Stream {
public static void main(String[] args) {
skip("", "baccad");
System.out.println(skip_Method2("baccdah"));
System.out.println(skip_apple("bacapplecdah"));
System.out.println(skipAppNotApple("bacapplcdah"));
}
// in this method ans is used in the arguments.
static void skip(String p, String up ){ // p = processed, up = unprocessed.
// base condition.
if (up.isEmpty()){
System.out.println(p);
return;
}
char ch = up.charAt(0);
if (ch == 'a'){ // if the char at 0th index is a then we don't need to add it in our processed string.
skip(p, up.substring(1));
}else { // else add that char into the processed string.
skip(p + ch, up.substring(1));
}
}
// ans is used in the body of the recursive call.
static String skip_Method2(String up ){ // p = processed, up = unprocessed.
// base condition.
if (up.isEmpty()){
return "";
}
char ch = up.charAt(0);
if (ch == 'a'){ // if the char at 0th index is a then we don't need to add it in our processed string.
return skip_Method2(up.substring(1));
}else { // else add that char into the processed string.
return ch + skip_Method2(up.substring(1));
}
}
// skips a string
static String skip_apple(String up ){ // p = processed, up = unprocessed.
// base condition.
if (up.isEmpty()){
return "";
}
if (up.startsWith("apple")){ // if the char at 0th index is a then we don't need to add it in our processed string.
return skip_apple(up.substring(5)); // because apple has 5 characters from index 0 to 4 so begin substring with 5th index
}else { // else add that char into the processed string.
return up.charAt(0) + skip_apple(up.substring(1));
}
}
// Skip a string if it is not required string.
// it will remove app only if there is not apple in the string.
static String skipAppNotApple(String up ){ // p = processed, up = unprocessed.
// base condition.
if (up.isEmpty()){
return "";
}
if (up.startsWith("app") && !up.startsWith("apple")){ // if the char at 0th index is a then we don't need to add it in our processed string.
return skipAppNotApple(up.substring(3)); // because apple has 5 characters from index 0 to 4 so begin substring with 5th index
}else { // else add that char into the processed string.
return up.charAt(0) + skipAppNotApple(up.substring(1));
}
}
}