-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW2Q4.cpp
More file actions
31 lines (29 loc) · 734 Bytes
/
W2Q4.cpp
File metadata and controls
31 lines (29 loc) · 734 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
31
class Solution {
public:
int compress(vector<char>& chars) {
int n= chars.size();
vector<char>tchars;
int i=0;
int x=0;
while(i<n){
char currentChar = chars[i];
int j=i;
int count=0;
tchars.push_back(currentChar);
x++;
while(i<n && chars[i]==currentChar ){
count++;
i++;
}
if (count > 1) {
string countStr = to_string(count);
for (char c : countStr) {
tchars.push_back(c);
x++;
}
}
}
chars.swap(tchars);
return x ;
}
};