public class roughLargestSmallest {
public static void main(String[] args) {
System.out.println(" Enter a sentence ");
String x = IO.readString();
compress(x);
}
public static String compress(String original) {
int count = 1;
StringBuilder builder = new StringBuilder();
for (int i = 1; i < original.length() - 1; i++) {
if (original.charAt(i) == original.charAt(i - 1)) {
count++;
} else {
builder.append(original.charAt(i - 1));
builder.append(count);
count = 1;
}
}
if (original.length() > 1) {
if (original.charAt(original.length() - 1) == original.charAt(original.length() - 2)) {
count++;
} else {
builder.append(original.charAt(original.length() - 2));
builder.append(count);
count = 1;
}
builder.append(original.charAt(original.length() - 1));
builder.append(count);
}
System.out.println(builder);
return builder.toString();
}
}
Attached is my code, it passes some of the test cases, but not the most. can anyone help me figure what i'm doing wrong? thanks!