File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int compress (vector<char >& chars) {
4
+ int n = (int )chars.size ();
5
+ if (chars.empty ()) return 0 ;
6
+ int left = 0 , right = 0 , currCharIndx = left;
7
+ while (right < n) {
8
+ if (chars[currCharIndx] != chars[right]) {
9
+ int len = right - currCharIndx;
10
+ chars[left++] = chars[currCharIndx];
11
+ if (len > 1 ) {
12
+ string freq = to_string (len);
13
+ for (int i = 0 ; i < (int )freq.length (); i++) {
14
+ chars[left++] = freq[i];
15
+ }
16
+ }
17
+ currCharIndx = right;
18
+ }
19
+ right++;
20
+ }
21
+
22
+ int len = right - currCharIndx;
23
+ chars[left++] = chars[currCharIndx];
24
+ if (len > 1 ) {
25
+ string freq = to_string (len);
26
+ for (int i = 0 ; i < freq.length (); i++) {
27
+ chars[left++] = freq[i];
28
+ }
29
+ }
30
+ return left;
31
+ }
32
+ };
You can’t perform that action at this time.
0 commit comments