Skip to content

Commit f2988b7

Browse files
Merge pull request codemistic#638 from GaurishIIITNR/PR5
Added Leetcode String Problem 443
2 parents 30759cd + 39f1d20 commit f2988b7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

String/443_String_Compression.c++

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
};

0 commit comments

Comments
 (0)