a----abbbbc -> ++++++++++c
把所有连续相同字母改成加号
同时dash 两侧如果有相同字符,则也整体变为加号
static String method(String s) {
char[] cs = s.toCharArray();
for (int i= 0; i < cs.length; i++) {
if (cs[i] == '-') {
int start = i;
int j = i;
while (j < cs.length && cs[j] == '-') {
j++;
}
if (j < cs.length && start-1 >= 0 && cs[start-1] == cs[j]) {
change(cs, i, j-1, cs[j]);
}
i = j;
}
}
int index = 0;
while (cs[index] =='-') {
index++;
}
if (index >= cs.length) {
return new String(cs);
}
char c = cs[index];
int cnt = 1;
for (int i = index+1; i < cs.length; i++) {
if (cs[i] == c) {
cnt++;
if(i == cs.length-1) {
change(cs, i-cnt+1, i, '+');
}
} else {
if(cnt>1) {
change(cs, i-cnt, i-1, '+');
}
if (cs[i] !='-') {
c = cs[i];
cnt = 1;
} else {
while (i < cs.length && cs[i] == '-') {
i++;
}
if (i < cs.length) {
c = cs[i];
cnt = 1;
}
}
}
}
return new String(cs);
}
static void change(char[] cs, int s, int e, char c) {
for (int i = s; i <= e; i++) {
cs[i] = c;
}
}
网友评论