-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path443.cpp
More file actions
30 lines (30 loc) · 826 Bytes
/
443.cpp
File metadata and controls
30 lines (30 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int compress(vector<char>& chars) {
int res = 0;
int n = chars.size();
int index = 0;
int charIdx = 0;
while (index < n) {
char c = chars[index];
int start = index;
while (index + 1 < n && chars[start] == chars[index + 1]) index++;
int count = index - start + 1;
if (count == 1) {
chars[charIdx++] = c;
res += 1;
}
else {
chars[charIdx++] = c;
res += 1;
string temp = to_string(count);
res += temp.size();
for (auto k : temp) {
chars[charIdx++] = k;
}
}
index++;
}
return res;
}
};