forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString Compression.cpp
54 lines (39 loc) · 1.11 KB
/
String Compression.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
void add1(vector<int>& arr) {
if(arr.back() < 9) {
arr.back()++;
return ;
}
reverse(begin(arr),end(arr));
int carry = 1;
for(int i=0;i<arr.size();i++) {
if(arr[i] < 9) {arr[i]++;carry=0;break;}
arr[i] = 0;
carry = 1;
}
if(carry == 1) arr.push_back(1);
reverse(begin(arr),end(arr));
}
int compress(vector<char>& chars) {
int i=0;
for(int j=0;j<chars.size();j++) {
if(j == chars.size()-1 or chars[j] != chars[j+1]) {
chars[i++] = chars[j];
}
else {
vector<int> cnt{0};
char ch = chars[j];
while(j < chars.size()and chars[j] == ch) {
j++;
add1(cnt);
}
j--; // bcoz j will be incremented in for loop updation condition.
chars[i++] = ch;
for(auto& it:cnt)
chars[i++] = '0'+it;
}
}
chars.erase(chars.begin()+i,chars.end());
return i;
}