Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created July 21, 2025 21:59
Show Gist options
  • Save Ifihan/59227438f971fc5b7c1efc13602056e9 to your computer and use it in GitHub Desktop.
Save Ifihan/59227438f971fc5b7c1efc13602056e9 to your computer and use it in GitHub Desktop.
Delete Characters to Make Fancy String

Question

Approach

I iterated through the string character by character while building a new result string. I kept track of the last two characters in the result. If the current character is the same as the last two added characters, I skipped it because adding it would create three consecutive identical characters, which violates the "fancy" condition. Otherwise, I added the character to the result.

Implementation

class Solution:
    def makeFancyString(self, s: str) -> str:
        result = []
        for char in s:
            if len(result) >= 2 and result[-1] == result[-2] == char:
                continue
            result.append(char)
        return ''.join(result)

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment