My task was to implement a method which performs basic string compression by counting sequences of repeating characters. Given "aaabbbccc"
it should return "a3b3c3"
. I have included some sample tests I made up. Please let me know if I have missed any cases. I am looking for the fastest implementation possible with the most concise code. I'm looking to cut down on if
else
statements or give them simpler logic if possible.
public class StringCompression {
public String compress(String str){
int count = 1;
StringBuilder builder = new StringBuilder();
for(int i = 1; i<str.length(); i++){
if(str.charAt(i) == str.charAt(i-1) && i < str.length()-1){
count++;
}
// case when the last letter is in the sequence preceding it. Add that sequence to
// the compressed string
else if(i == str.length()-1 && str.charAt(i) == str.charAt(i-1)){
count++;
builder.append(str.charAt(i));
builder.append(count);
}
// case where the last letter is NOT in the sequence preceding it. Add it to string.
else if(i == str.length()-1 && str.charAt(i) != str.charAt(i-1)){
builder.append(str.charAt(i-1));
builder.append(count);
count = 1;
builder.append(str.charAt(i));
builder.append(count);
}
else{
// appending the character and THEN appending the count works.
builder.append(str.charAt(i-1));
builder.append(count);
count = 1;
}
}
str = builder.toString();
System.out.println(str);
return str;
}
public static void main(String[] args){
StringCompression test = new StringCompression();
test.compress("aabcccccaaa");
test.compress("aaaaa");
test.compress("aaaabbb");
test.compress("aaabbbccc");
test.compress("abc");
test.compress("a");
test.compress("");
}
}