-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-029.cpp
51 lines (48 loc) · 1.34 KB
/
Day-029.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
/*
This problem was asked by Amazon.
Run-length encoding is a fast and simple method of encoding strings.
The basic idea is to represent repeated successive characters as a single count and character.
For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
Implement run-length encoding and decoding.
You can assume the string to be encoded have no digits and consists solely of alphabetic characters.
You can assume the string to be decoded is valid.
*
*
*
*/
#include <bits/stdc++.h>
using namespace std;
string encode(string &s){
string res="";
for(int i=0;i<(int)s.size();++i){
int j = i;
int counter{};
while(s[i]==s[j]){
++counter;
++j;
}
res += to_string(counter) + s[i];
i = (j-1);
}
return res;
}
string decode(string &s){
string res = "";
for(int i=0;i<(int)s.size();++i){
int counter{};
while(s[i]>='0' && s[i]<='9'){
counter = (counter*10)+int(int(s[i])-int('0'));
++i;
}
res += string(counter , s[i]);
}
return res;
}
int main(void){
string text = "AAAABBBCCDAA";
string encoded_string = encode(text);
cout<<encoded_string<<'\n';
string decoded_string = decode(encoded_string);
cout<<decoded_string<<'\n';
return 0;
}