-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLengthEncoding.cpp
More file actions
72 lines (58 loc) · 1.76 KB
/
RunLengthEncoding.cpp
File metadata and controls
72 lines (58 loc) · 1.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*------------------------------------------------------------------------------------------------------------------------------------------------
Problem statement:
You have to show the count of each character in string and append that count with that character in same string, you will get output string.
After that your are provided with one token string which will be appended with output and then replace each third character of output string
with "X" to get final output.
----EXAMPLE:
input=aaamnna
output=3a1m2n1a
output=3a1m2n1aTokenStr
finalOutput=3aXm2X1aXokXnSXr
---------------------------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string fun(string str) {
vector<string> v;
for (int i = 0; i < str.length(); i++) {
int count = 1;
//getting count of consecutive same characters
while (str[i] == str[i + 1] && i < str.length() - 1) {
count++;
i++;
}
//converting character to string
string s;
s.push_back(str[i]);
//appending character with its count
s = to_string(count) + s;
//storing result in vector
v.push_back(s);
}
//coverting vector result to string
string output = "";
for (int i = 0; i < v.size(); i++) {
output = output + v[i];
}
string finalOutput = "";
//appending with token
output = output + "k78zb6yca0";
//Replace each third character of output string with "X"
for (int i = 0; i < output.length(); i++) {
if ((i+1) % 3 == 0 && i!=0) {
finalOutput = finalOutput + "X";
}
else {
finalOutput = finalOutput + output[i];
}
}
return finalOutput;
}
int main() {
string input;
cout << "input:";
cin >> input;
cout << fun(input);
return 0;
}