-
Notifications
You must be signed in to change notification settings - Fork 0
/
2Runtime.cpp
42 lines (38 loc) · 916 Bytes
/
2Runtime.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
// Run Lenght Encoding Time Complexicity is Linear Itself Only.
#include <iostream>
using namespace std;
// Run-Length-Encoding for String Compression
// if Compressed string is Bigger than Orignal return orignal string
// input : aaabbccddee
// Output : a3b2c2d2e2
// Input : abcd
// Output : abcd
string compressString(string str)
{
int n = str.length();
string output;
for (int i = 0; i < n; i++)
{
int count = 1;
while (i < n - 1 and str[i + 1] == str[i])
{
count++;
i++;
}
output += str[i]; // += Used for Merge(Concatination)
output += to_string(count);
}
if (output.length() > str.length())
{
return str;
}
return output;
}
int main()
{
string s1 = "aaabbccddee";
cout << compressString(s1) << endl;
string s2 = "abcd";
cout << compressString(s2) << endl;
return 0;
}