forked from xperzy/careerup150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-5.cpp
53 lines (47 loc) · 887 Bytes
/
1-5.cpp
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
49
50
51
52
53
#include <iostream>
#include <string>
using namespace std;
class Solution{
public:
string compress(string str){
string res="";
if (str.size()==0) {return str;}
char last = str[0];
int count = 0;
for (int i=0;i<str.size();i++){
if (str[i]==last){
count ++;
}else{
res = res + last;
if (count >1){
string cst = "";
while(count!=0){
cst = char(count%10+'0')+cst;
count = count/10;
}
res = res+cst;
}
count = 1;
last = str[i];
}
}
string cst = "";
if (count >1){
while(count!=0){
cst = char(count%10+'0')+cst;
count = count/10;
}
}
res = res + last + cst;
return res;
}
};
int main(){
Solution sol;
//string str1 = "aaaabbcccddeeaaaaaaaavbaaaa";
string str1 = "abcdefg";
string str2 = sol.compress(str1);
cout << str1 << endl;
cout << str2 << endl;
return 0;
}