-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_1957.cpp
More file actions
87 lines (77 loc) · 2.01 KB
/
Leetcode_1957.cpp
File metadata and controls
87 lines (77 loc) · 2.01 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 1957. Delete Characters to Make Fancy String
// link - https://leetcode.com/problems/delete-characters-to-make-fancy-string/description
/*
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 <= 10^5
s consists only of lowercase English letters.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string makeFancyString(string s)
{
string ans;
int count = 0;
char curr = s[0];
for (char c : s)
{
if (c == curr && count >= 2)
{
continue;
}
ans.push_back(c);
if (c == curr)
{
count++;
}
else
{
curr = c;
count = 1;
}
}
return ans;
}
int main()
{
vector<pair<string, string>> testcases = {
{"leeetcode", "leetcode"},
{"aaabaaaa", "aabaa"},
{"aab", "aab"},
{"aaa", "aa"},
{"abc", "abc"},
{"a", "a"},
{"bbbaaabb", "bbaabb"}};
for (auto &[input, expected] : testcases)
{
string result = makeFancyString(input);
cout << "Input: " << input << "\nOutput: " << result << "\nExpected: " << expected << "\n";
cout << (result == expected ? "PASS" : "FAIL") << "\n\n";
}
return 0;
}
/*
Time Complexity: O(N), where N is the length of s
Space Complexity: O(N) for the answer string
*/