MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / compressString

Function compressString

CPP/char array/lengthencode.cpp:8–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#define mod 1000000007
7#define endl '\n'
8string compressString(string s){
9 int n=s.length();
10 string ans;
11 for (int i = 0; i < n; i++) {
12 int count=1;
13 while(s[i]==s[i+1] && i<n-1){
14 count++;
15 i++;
16 }
17 ans+=s[i];
18 ans+=to_string(count);
19 }
20 if(ans.length()<s.length())
21 return ans;
22 else
23 return s;
24
25}
26int main(){
27 cin.tie(0)->sync_with_stdio(0);
28 string s1 ="aaabbccddee";

Callers 1

mainFunction · 0.85

Calls 1

lengthMethod · 0.80

Tested by

no test coverage detected