Essentially, what this code does is:
- Take an input.
- Replace each sequence of characters whose length is greater than 2 with the number of times that character repeated and the character itself (e.g.
jjjkkkkkllll
=3j5k4l
). The input does not contain any numeric values. - Return the result.
The code:
private String replaceConsecutiveChars(String data) {
char[] dataChars = data.toCharArray();
int i = 0;
int k = 0;
Character charType = null;
for(Character c : dataChars) {
if(k == dataChars.length - 1 && i >= 2) {
data = data.replace(repeat(String.valueOf(charType), ++i), (i + Character.toString(charType)));
break;
}
if(i == 0) {
charType = c;
i++;
}else if(c == charType) {
i++;
}else if(c != charType && i > 2) {
data = data.replace(repeat(String.valueOf(charType), i), (i + Character.toString(charType)));
i = 1;
charType = c;
}else if(c != charType && i <= 2) {
i = 1;
charType = c;
}
k++;
}
return data;
}
private String repeat(String s, int n) {
return Stream.generate(() -> s).limit(n).collect(Collectors.joining(""));
}
However, my implementation only seems to work with the limited-ASCII character set, but I am trying to get it work with the Unicode character set. For example:
- The input
ddddddddkkkkkpppp
will correctly output8d5k4p
. - The input
êêêêÌÌÌÌÌÌÌØØØ
will incorrectly outputêêêêÌÌÌÌÌÌÌØØØ
- The input
"rrrrrêêêêÌÌÌÌÌkkkkØØØ"
will incorrectly output5rêêêêÌÌÌÌÌ4kØØØ
Why is this?
In addition, is there a better way I could do this than the way I'm doing it right now?