Magicsheet logo

Delete Characters to Make Fancy String

Easy
92%
Updated 6/1/2025

Delete Characters to Make Fancy String

What is this problem about?

The Delete Characters to Make Fancy String interview question asks you to modify a string such that no character appears three times in a row. You should delete the minimum number of characters possible. This Delete Characters to Make Fancy String coding problem is a test of linear string filtering.

Why is this asked in interviews?

Companies like Meta and Wayfair use this "Easy" question to evaluate a candidate's basic string manipulation and "clean code" practices. It tests if you can correctly identify a streak of characters and apply a simple exclusion rule without overcomplicating the logic.

Algorithmic pattern used

This follows the String interview pattern.

  1. Iterate through the string from left to right.
  2. Keep track of the current character and its consecutive count.
  3. If the count is less than 3, add the character to your result.
  4. Alternative: Check the last two characters added to the result. If both are equal to the current character, skip the current one.

Example explanation

s = "leeetccode"

  1. 'l': Result "l"
  2. 'e': Result "le"
  3. 'e': Result "lee"
  4. 'e': The last two in Result are "ee". Skip this one.
  5. 't': Result "leet" ... and so on. Result: "leetcode".

Common mistakes candidates make

  • String Immutability: Using s = s.substring(0, i) + s.substring(i + 1) which makes the solution O(N^2).
  • Index Management: Trying to delete characters while the loop is running, which can lead to skipping characters or index-out-of-bounds.
  • Complex Streaks: Trying to use nested loops to count streaks when a simple comparison with the last two elements is sufficient.

Interview preparation tip

Always use a StringBuilder or an array of characters for string-building tasks. It's much more efficient than repeated concatenation and shows you understand low-level memory performance.

Similar Questions