Skip to content

Commit 34e4e0f

Browse files
authored
Implement string compression algorithm
1 parent 223b0cf commit 34e4e0f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
string str;
6+
cin >> str;
7+
8+
int n = str.length();
9+
10+
for (int i = 0; i < n; i++) {
11+
int count = 1;
12+
13+
// Count same consecutive characters
14+
while (i < n - 1 && str[i] == str[i + 1]) {
15+
count++;
16+
i++;
17+
}
18+
19+
cout << str[i] << count;
20+
}
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)