[Solution] Delete Characters to Make Fancy String solution leetcode

Delete Characters to Make Fancy String solution leetcode


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: Delete Characters to Make Fancy String solution leetcode

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: Delete Characters to Make Fancy String solution leetcode

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: Delete Characters to Make Fancy String solution leetcode

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.

Solution: Click here


This is 58th LeetCode Biweekly Contest.  LeetCode heavily emphasizes on the justice and fairness of our contests. LeetCode have fully intolerance for violation behaviors (such as plagiarism, cheating, etc). once a user is deemed violating contest rules, LeetCode will be going to apply the subsequent penalties on this user:

  1. First violation: LeetCoin quantity resets to zero and a contest and discuss ban for one month.
  2. Second violation: Permanent account deactivation while not charm.

 

Leave a Comment