I find the code to be a little hard to read because of the many variables. You only really need two counters, start
and end
, so we can trim out the rest--as a rule, fewer variables ⇒ less code to update them ⇒ fewer bugs. I tried slimming it down, and ended up with this:
static String nextLine(String prev) {
final StringBuilder sb = new StringBuilder();
int start = 0;
while ( start < prev.length() ) {
int end = start + 1;
while ( end < prev.length() && prev.charAt(end) == prev.charAt(start) ) {
end++;
}
sb.append(end - start).append(prev.charAt(start));
start = end;
}
return sb.toString();
}
Some quick timing taught me that rolfl's snippet rolfl's snippet was about 5% faster overall (Oracle Java build 1.8.0-b132, 64-bit server), but so far both your/mine as rolfl's approach suffer from a major problem: we blow through all our heap space by about the 70th sequence, so efficiency is a matter of perspective.
I find the code to be a little hard to read because of the many variables. You only really need two counters, start
and end
, so we can trim out the rest--as a rule, fewer variables ⇒ less code to update them ⇒ fewer bugs. I tried slimming it down, and ended up with this:
static String nextLine(String prev) {
final StringBuilder sb = new StringBuilder();
int start = 0;
while ( start < prev.length() ) {
int end = start + 1;
while ( end < prev.length() && prev.charAt(end) == prev.charAt(start) ) {
end++;
}
sb.append(end - start).append(prev.charAt(start));
start = end;
}
return sb.toString();
}
Some quick timing taught me that rolfl's snippet was about 5% faster overall (Oracle Java build 1.8.0-b132, 64-bit server), but so far both your/mine as rolfl's approach suffer from a major problem: we blow through all our heap space by about the 70th sequence, so efficiency is a matter of perspective.
I find the code to be a little hard to read because of the many variables. You only really need two counters, start
and end
, so we can trim out the rest--as a rule, fewer variables ⇒ less code to update them ⇒ fewer bugs. I tried slimming it down, and ended up with this:
static String nextLine(String prev) {
final StringBuilder sb = new StringBuilder();
int start = 0;
while ( start < prev.length() ) {
int end = start + 1;
while ( end < prev.length() && prev.charAt(end) == prev.charAt(start) ) {
end++;
}
sb.append(end - start).append(prev.charAt(start));
start = end;
}
return sb.toString();
}
Some quick timing taught me that rolfl's snippet was about 5% faster overall (Oracle Java build 1.8.0-b132, 64-bit server), but so far both your/mine as rolfl's approach suffer from a major problem: we blow through all our heap space by about the 70th sequence, so efficiency is a matter of perspective.
I find the code to be a little hard to read because of the many variables. You only really need two counters, start
and end
, so we can trim out the rest--as a rule, fewer variables ⇒ less code to update them ⇒ fewer bugs. I tried slimming it down, and ended up with this:
static String nextLine(String prev) {
final StringBuilder sb = new StringBuilder();
int start = 0;
while ( start < prev.length() ) {
int end = start + 1;
while ( end < prev.length() && prev.charAt(end) == prev.charAt(start) ) {
end++;
}
sb.append(end - start).append(prev.charAt(start));
start = end;
}
return sb.toString();
}
Some quick timing taught me that rolfl's snippet was about 5% faster overall (Oracle Java build 1.8.0-b132, 64-bit server), but so far both your/mine as rolfl's approach suffer from a major problem: we blow through all our heap space by about the 70th sequence, so efficiency is a matter of perspective.