File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ // Name :String Compression
2+ // Url: https://leetcode.com/problems/string-compression/
3+ // Tag : String
4+
5+ class Solution
6+ {
7+ public:
8+ int compress (vector<char > &chars)
9+ {
10+
11+ char temp = chars[0 ];
12+ int cnt = 0 , n = chars.size ();
13+ for (int i = 0 ; i < n; i++)
14+ {
15+ if (chars[i] == temp)
16+ cnt++;
17+ else
18+ {
19+ chars.erase (chars.begin (), chars.begin () + i);
20+ i = 0 ;
21+ n -= cnt;
22+ chars.push_back (temp);
23+ if (cnt != 1 )
24+ {
25+ string k = to_string (cnt);
26+ for (auto x : k)
27+ chars.push_back (x);
28+ }
29+ temp = chars[i];
30+ cnt = 1 ;
31+ continue ;
32+ }
33+ }
34+ chars.erase (chars.begin (), chars.begin () + cnt);
35+ chars.push_back (temp);
36+ if (cnt != 1 )
37+ {
38+ string k = to_string (cnt);
39+ for (auto x : k)
40+ chars.push_back (x);
41+ }
42+ return chars.size ();
43+ }
44+ };
You can’t perform that action at this time.
0 commit comments