-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily137.cpp
More file actions
45 lines (38 loc) · 849 Bytes
/
Copy pathdaily137.cpp
File metadata and controls
45 lines (38 loc) · 849 Bytes
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
// Solution 1
class Solution {
public:
string makeFancyString(string s) {
if (s.size() <= 2)
return s;
auto res = std::string{s[0]};
char prev = s[0];
int count = 1;
for (auto i = 1; i < s.size(); ++i) {
if (s[i] == prev) {
count++;
} else
count = 1;
if (count == 3) {
count--;
} else
res += s[i];
prev = s[i];
}
return res;
}
};
// Solution 2
class Solution {
public:
string makeFancyString(string s) {
if(s.length()<3) {
return s;
}
int j = 2;
for (int i = 2; i < s.size(); ++i)
if (s[i] != s[j - 1] || s[i] != s[j - 2])
s[j++] = s[i];
s.resize(j);
return s;
}
};