-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoString.java
More file actions
64 lines (46 loc) · 1.12 KB
/
Copy pathTwoString.java
File metadata and controls
64 lines (46 loc) · 1.12 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
/*
package rank;
*/
/**
* created by @author suraj on 05/11/19
*//*
public class TwoString {
static int max = Integer.MIN_VALUE;
static int alternate(String s) {
int i = 0;
while (i < s.length()) {
performAlgo(s, i);
i++;
}
return Math.max(max, 0);
}
static void performAlgo(String s, int pos) {
if (s.length() == 1) {
max = Math.max(max, s.length());
return;
}
if (checkIsAlternating(s)) {
max = Math.max(max, s.length());
}
if (pos > s.length() - 1) {
return;
}
s = s.replaceAll((s.charAt(pos) + ""), "");
performAlgo(s, pos);
}
static boolean checkIsAlternating(String s) {
if (s.length() == 1) {
return true;
}
if (s.length() == 2) {
return s.charAt(0) == s.charAt((1));
}
for (int i = 0; i < s.length(); i++) {
if (i + 2 < s.length() && s.charAt(i) != s.charAt(i + 2)) {
return false;
}
}
return true;
}
}
*/