-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily140.cpp
More file actions
52 lines (48 loc) · 1.24 KB
/
Copy pathdaily140.cpp
File metadata and controls
52 lines (48 loc) · 1.24 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
// Solution 1
class Solution {
public:
string compressedString(string word) {
auto comp = std::string{};
char curr_char = word[0];
auto count = 1;
for (auto i = 1; i < word.size(); ++i) {
if (curr_char == word[i] and count < 9)
count++;
else {
comp += std::to_string(count) + curr_char;
curr_char = word[i];
count = 1;
}
}
return comp + std::to_string(count) + curr_char;
}
};
// Solution 2
class Solution {
public:
string compressedString(string word) {
string temp;
char last=word[0];
int cnt=1;
for(int i=1; i<word.size(); i++){
if(last==word[i]){
if(cnt<9){
++cnt;
continue;
}else{
temp.push_back(cnt+'0');
temp.push_back(last);
cnt=1;
}
}else{
temp.push_back(cnt+'0');
temp.push_back(last);
cnt=1;
last=word[i];
}
}
temp.push_back(cnt+'0');
temp.push_back(last);
return temp;
}
};