Skip to content

Commit dfd7f13

Browse files
authored
String Compression Solution Added
1 parent d10046d commit dfd7f13

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Hacker Blocks/String Compression.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Hacker Blocks */
2+
/* Title - String Compression */
3+
/* Created By - Akash Modak */
4+
/* Date - 23/6/2020 */
5+
6+
// Take as input S, a string. Write a function that does basic string compression. Print the value returned. E.g. for input “aaabbccds” print out a3b2c2d1s1.
7+
8+
// Input Format
9+
// A single String S
10+
11+
// Constraints
12+
// 1 < = length of String < = 1000
13+
14+
// Output Format
15+
// The compressed String.
16+
17+
// Sample Input
18+
// aaabbccds
19+
// Sample Output
20+
// a3b2c2d1s1
21+
// Explanation
22+
// In the given sample test case 'a' is repeated 3 times consecutively, 'b' is repeated twice, 'c' is repeated twice and 'd and 's' occurred only once.
23+
24+
#include<iostream>
25+
using namespace std;
26+
void compressed()
27+
{
28+
string str;
29+
cin>>str;
30+
int n = str.length();
31+
int i=0;
32+
int count = 0;
33+
char current;
34+
while(i<n){
35+
if(count==0){
36+
count++;
37+
current = str[i];
38+
cout<<str[i];
39+
40+
}
41+
else if(current==str[i]){
42+
count++;
43+
}
44+
else{
45+
cout<<count;
46+
cout<<str[i];
47+
current=str[i];
48+
count=1;
49+
}
50+
i++;
51+
}
52+
if(n>=1)
53+
cout<<count<<endl;
54+
cout<<endl;
55+
}
56+
int main() {
57+
compressed();
58+
return 0;
59+
}

0 commit comments

Comments
 (0)