-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular-compress-string .cpp
More file actions
48 lines (39 loc) · 1.09 KB
/
regular-compress-string .cpp
File metadata and controls
48 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
//Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3.
//If the "compressed" string would not become smaller than the original string,your method should return the original string.
//
//intput: aabcccccaaa
//output: a2b1c5a3
//
//input: aaaaabcdefa --/--> a5b1c1d1e1f1a1
//output: aaaaabcdefa
#include <iostream>
#include <string>
using namespace std;
string compress(const string &s){
char currchar = s[0];
int count = 1;
string res = "";
for(int i=1; i<=s.size(); i++){
if(s[i]==currchar && i != s.size()){
count++;
}
else{
res += currchar;
res.append(to_string(count));
count = 1;
currchar = s[i];
}
}
return (res.size()<s.size()) ? res : s;
}
int main()
{
string s = "aabcccccaaa";
string data = compress(s);
cout << data;
string s = "aaaaabcdefa";
string data = compress(s);
cout << data;
return 0;
}