Talentd
FleetCode

1957. Delete Characters to Make Fancy String

Easy74.1% acceptance6 companies

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.

Example 1:

Input: s = "leeetcode"
Output: "leetcode"
Explanation:
Remove an 'e' from the first group of 'e's to create "leetcode".
No three consecutive characters are equal, so return "leetcode".

Example 2:

Input: s = "aaabaaaa"
Output: "aabaa"
Explanation:
Remove an 'a' from the first group of 'a's to create "aabaaaa".
Remove two 'a's from the second group of 'a's to create "aabaa".
No three consecutive characters are equal, so return "aabaa".

Example 3:

Input: s = "aab"
Output: "aab"
Explanation: No three consecutive characters are equal, so return "aab".

Constraints:

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

Topics

Hints open one at a time

Hint 1
What's the optimal way to delete characters if three or more consecutive characters are equal?
Hint 2
If three or more consecutive characters are equal, keep two of them and delete the rest.
7:14
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def makeFancyString(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"leeetcode"
Input
"aaabaaaa"
Input
"aab"
Your code is saved on this device.Solution