class Solution {
public:
int compress(vector<char>& chars) {
int i = 0;
int j = 0;
int k = 0;
while(true){
if (k<chars.size() && chars[j] == chars[k]){
k++;
continue;
}
int num = k - j;
chars[i++] = chars[j];
j = k;
if (num == 1){
if (k == chars.size()){
break;
}
continue;
}
string num_str = to_string(num);
for (auto c : num_str){
chars[i++] = c;
}
if (k == chars.size()){
break;
}
}
return i;
}
};