Skip to content

Commit aff6e89

Browse files
authored
Create 3163. String Compression III (#625)
2 parents d449a3b + 66d1104 commit aff6e89

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

3163. String Compression III

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string compressedString(string word)
4+
{
5+
string ans;
6+
7+
int count = 0;
8+
char prev = word[0];
9+
for(auto ch : word)
10+
{
11+
if(prev != ch or count == 9)
12+
{
13+
ans += to_string(count);
14+
ans += prev;
15+
prev = ch;
16+
count = 0;
17+
}
18+
count += 1;
19+
}
20+
if(count)
21+
{
22+
ans += to_string(count);
23+
ans += prev;
24+
}
25+
return ans;
26+
}
27+
};

0 commit comments

Comments
 (0)