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.
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)- Time: O(n)
- Space: O(n)