Skip to content

Commit 59af794

Browse files
committed
Time: 11 ms (31.21%), Space: 8.9 MB (93.92%) - LeetHub
1 parent 3776ab7 commit 59af794

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int compress(vector<char>& chars) {
4+
int i = 0, ansIndex = 0;
5+
int n = chars.size();
6+
while(i<n){
7+
int j = i+1;
8+
while(j<n && chars[i] == chars[j]){
9+
j++;
10+
}
11+
12+
// ya toh vector pura traverse krr diyaa
13+
// ya fir new/Different character encounter kia hai
14+
15+
chars[ansIndex++] = chars[i];
16+
int count = j-i;
17+
if(count > 1){
18+
// converting counting into single digit and saving in answer
19+
string cnt = to_string(count);
20+
for(char ch: cnt){
21+
chars[ansIndex++] = ch;
22+
}
23+
}
24+
i = j;
25+
}
26+
return ansIndex;
27+
}
28+
};

0 commit comments

Comments
 (0)