-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsbc test function
66 lines (55 loc) · 1.93 KB
/
hsbc test function
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
这个类用于移除连续三个或更多相同字符的字符串:
A
package com.example;
public class ConsecutiveRemover {
public String removeConsecutive(String input) {
if (input == null || input.isEmpty()) return input;
StringBuilder sb = new StringBuilder(input);
boolean changed;
do {
changed = false;
for (int i = 0; i < sb.length() - 2; ) {
if (sb.charAt(i) == sb.charAt(i + 1) && sb.charAt(i) == sb.charAt(i + 2)) {
char toRemove = sb.charAt(i);
int j = i;
while (j < sb.length() && sb.charAt(j) == toRemove) {
j++;
}
sb.delete(i, j);
changed = true;
} else {
i++;
}
}
} while (changed);
return sb.toString();
}
}
B
这个类用于替换连续三个或更多相同字符为它们前一个字母:
package com.example;
public class ConsecutiveReplacer {
public String replaceConsecutive(String input) {
if (input == null || input.isEmpty()) return input;
StringBuilder sb = new StringBuilder(input);
boolean changed;
do {
changed = false;
for (int i = 0; i < sb.length() - 2; ) {
if (sb.charAt(i) == sb.charAt(i + 1) && sb.charAt(i) == sb.charAt(i + 2)) {
char toReplace = sb.charAt(i);
char replacement = (char) (toReplace - 1);
int j = i;
while (j < sb.length() && sb.charAt(j) == toReplace) {
j++;
}
sb.replace(i, j, String.valueOf(replacement));
changed = true;
} else {
i++;
}
}
} while (changed);
return sb.toString();
}
}