Delete Characters to Make Fancy String - Problem

A fancy string is a string where no three consecutive characters are equal.

Given a string s, delete the minimum possible number of characters from s to make it fancy.

Return the final string after the deletion. It can be shown that the answer will always be unique.

Input & Output

Example 1 — Basic Case
$ Input: s = "leeetcode"
Output: "leetcode"
💡 Note: Remove one 'e' from the three consecutive 'e's. The result is "leetcode" with no three consecutive characters equal.
Example 2 — Multiple Groups
$ Input: s = "aaabaaaa"
Output: "aabaa"
💡 Note: Remove one 'a' from "aaa" at start and two 'a's from "aaaa" at end. Result: "aabaa".
Example 3 — Already Fancy
$ Input: s = "aab"
Output: "aab"
💡 Note: No three consecutive characters are equal, so no deletion needed.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists only of lowercase English letters

Visualization

Tap to expand
Making a Fancy String: "leeetcode" → "leetcode"Original StringleeetcodeDELETERemove 3rd consecutive 'e'Fancy StringleetcodeResult: No three consecutive characters!
Understanding the Visualization
1
Input
Original string with potential consecutive characters
2
Process
Remove minimum characters to avoid 3+ consecutive
3
Output
Fancy string with at most 2 consecutive characters
Key Takeaway
🎯 Key Insight: Only need to track the last 2 characters to prevent 3 consecutive duplicates
Asked in
Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen