public class Solution {
int len = str.length();
StringBuilder ans = new StringBuilder();
int cnt = 1;
for (int i = 1; i < len; i++) {
if (str.charAt(i) == str.charAt(i-1))
cnt++;
else {
ans.append(str.charAt(i-1));
if (cnt > 1) {
ans.append(cnt);
}
cnt = 1;
}
if (i == len - 1) {
ans.append(str.charAt(i));
if (cnt > 1) {
ans.append(cnt);
}
}
}
return ans.toString();
}
}
cHVibGljIGNsYXNzIFNvbHV0aW9uIHsKCXB1YmxpYyBzdGF0aWMgU3RyaW5nIGdldENvbXByZXNzZWRTdHJpbmcoU3RyaW5nIHN0cikgewoJCQoJCWludCBsZW4gPSBzdHIubGVuZ3RoKCk7CgoJCVN0cmluZ0J1aWxkZXIgYW5zID0gbmV3IFN0cmluZ0J1aWxkZXIoKTsKCgoJCWludCBjbnQgPSAxOwoKCQlmb3IgKGludCBpID0gMTsgaSA8IGxlbjsgaSsrKSB7CgoKCQkJaWYgKHN0ci5jaGFyQXQoaSkgPT0gc3RyLmNoYXJBdChpLTEpKQoJCQkJY250Kys7CgoKCQkJZWxzZSB7CgoJCQkJYW5zLmFwcGVuZChzdHIuY2hhckF0KGktMSkpOwoKCQkJCWlmIChjbnQgPiAxKSB7CgkJCQkJYW5zLmFwcGVuZChjbnQpOwoJCQkJfQoKCQkJCWNudCA9IDE7CgkJCX0KCgkJCWlmIChpID09IGxlbiAtIDEpIHsKCgkJCQlhbnMuYXBwZW5kKHN0ci5jaGFyQXQoaSkpOwoKCQkJCWlmIChjbnQgPiAxKSB7CgkJCQkJYW5zLmFwcGVuZChjbnQpOwoJCQkJfQoJCQl9CgkJfQoKCgkJcmV0dXJuIGFucy50b1N0cmluZygpOwoJfQoKfQ==