Basically just run a sliding window and check if the current letter has been repeated more than 3 times.
code
class Solution {
public:
string makeFancyString(string s) {
string ans{};
char prev{};
int curCounter{1};
for (char& c : s) {
if (c == prev && curCounter < 2) {
ans += c;
++curCounter;
} else if (c != prev) {
ans += c;
prev = c;
curCounter = 1;
}
}
return ans;
}
};
complexity
- Time:
O(n)wherenstands for the number of letterss- Space:
O(1)
time taken
- 2 hours, 35 minutes and 31 seconds
- AFKing might not be the play-