1957: Delete-Characters-to-Make-Fancy-String
Easy


table of contents

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 taken