-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_Delete_Characters_to_Make_Fancy_String.cpp
More file actions
64 lines (47 loc) · 1.55 KB
/
Copy path21_Delete_Characters_to_Make_Fancy_String.cpp
File metadata and controls
64 lines (47 loc) · 1.55 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
// 1957. Delete Characters to Make Fancy String
// A fancy string is a string where no three consecutive characters are equal.
// Given a string s, delete the minimum possible number of characters from s to make it fancy.
// Return the final string after the deletion. It can be shown that the answer will always be unique.
// Example 1:
// Input: s = "leeetcode"
// Output: "leetcode"
// Explanation:
// Remove an 'e' from the first group of 'e's to create "leetcode".
// No three consecutive characters are equal, so return "leetcode".
// Example 2:
// Input: s = "aaabaaaa"
// Output: "aabaa"
// Explanation:
// Remove an 'a' from the first group of 'a's to create "aabaaaa".
// Remove two 'a's from the second group of 'a's to create "aabaa".
// No three consecutive characters are equal, so return "aabaa".
// Example 3:
// Input: s = "aab"
// Output: "aab"
// Explanation: No three consecutive characters are equal, so return "aab".
// Constraints:
// 1 <= s.length <= 105
// s consists only of lowercase English letters.
class Solution
{
public:
string makeFancyString(string s)
{
vector<char> chars(s.begin(), s.end());
char last = chars[0];
int count = 1;
int pos = 1;
for (int i = 1; i < chars.size(); ++i)
{
if (chars[i] != last)
{
last = chars[i];
count = 0;
}
if (++count > 2)
continue;
chars[pos++] = chars[i];
}
return string(chars.begin(), chars.begin() + pos);
}
};