How to transform the string "AABSSSD" into "2AB3SD"?
In your else
part you should set counter to 1
instead of 0
as new character is having it's first occurrence,
else {
newStr.append(count);
newStr.append(temp);
count = 1;//Just change this
}
and replace 1
instead of 0
from the String
x = x.replace("1", "");
because 0A
does not look valid as A
occurred once in the String
so it should be 1A
instead of 0A
.