class Solution {
public String compressString(String S) {
int i=0;
int n = S.length();
StringBuilder result = new StringBuilder();
while(i < n){
int j = i;
while(j < n && S.charAt(i) == S.charAt(j)){
j++;
}
result.append(S.charAt(i));
result.append(j-i);
i=j;
}
String res = result.toString();
if (res.length() < S.length()) {
return res;
} else {
return S;
}
}
}