-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompressionFB.cpp
More file actions
49 lines (41 loc) · 1 KB
/
StringCompressionFB.cpp
File metadata and controls
49 lines (41 loc) · 1 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
49
#include <cstdlib>
#include <iostream>
using namespace std;
string strCompress(string input) {
if (input.length() == 0) {
return "";
}
int count = 1;
string result = "";
char temp = input[0];
for (int i = 1; i < input.size(); i++) {
if (input[i] == input[i - 1]) {
count ++;
} else {
if (count != 1) {
result += temp;
result += (char)('0' + count);
} else {
result += temp;
}
temp = input[i];
count = 1;
}
}
if (count != 1) {
result += temp;
result += (char)('0' + count);
} else {
result += temp;
}
return result;
}
int main(int argc, char *argv[])
{
string input[5] = {"", "a", "aaabb", "abbbcc", "abcd"};
for (int i = 0; i < 5; i++) {
cout << strCompress(input[i]) << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}