int compress(vector<char>& chars) {
if(chars.size() == 0) return 0;
chars.push_back(1);
char c_prev = 1;
int cnt = 0, k = 0;
for(int j=0; j<chars.size(); j++) {
if(chars[j] == c_prev) cnt++;
else {
if(cnt != 0) {
chars[k++] = c_prev;
if(cnt != 1) {
string s = to_string(cnt);
for(char cs: s) {
chars[k++] = cs;
}
}
}
c_prev = chars[j];
cnt = 1;
}
}
chars.pop_back();
return k;
}
No comments:
Post a Comment