Skip to content

← leetcode

Delete Characters to Make Fancy String

· Easy

LeetCode problem 1957

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) where n stands for the number of letters s
  • Space: O(1)

time taken

  • 2 hours, 35 minutes and 31 seconds
    • AFKing might not be the play-