-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringRecursion.java
More file actions
68 lines (48 loc) · 1.3 KB
/
StringRecursion.java
File metadata and controls
68 lines (48 loc) · 1.3 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
public class Solution {
public static String removeConsecutiveDuplicates(String s) {
// Sample Input 1 :
// aabccba
// Sample Output 1 :
// abcba
if(s.length()<=1){
return s;
}
if(s.charAt(0) == s.charAt(1)){
return removeConsecutiveDuplicates(s.substring(1));
}else{
return s.charAt(0) + removeConsecutiveDuplicates(s.substring(1));
}
}
public static String removeX(String s){
// Sample Input 1 :
// xaxb
// Sample Output 1:
// ab
if(s.length() == 0){
return s;
}
String rem = removeX(s.substring(1));
if(s.charAt(0)== 'x'){
return "" + rem;
}else{
return s.charAt(0) + rem;
}
}
public static boolean checkAB(String input) {
// Sample Input 1 :
// abb
// Sample Output 1 :
// true
if(input.length() == 0){
return true;
}
if(input.charAt(0) == 'a'){
if(input.substring(1).length() > 1 && input.substring(1,3).equals("bb")){
return checkAB(input.substring(3));
}else{
return checkAB(input.substring(1));
}
}
return false;
}
}