Skip to content

Commit

Permalink
string
Browse files Browse the repository at this point in the history
leetcode 110124
  • Loading branch information
Zhenye-Na committed Nov 1, 2024
1 parent 752add0 commit 6eb5e96
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions python/1957.delete-characters-to-make-fancy-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# @lc app=leetcode id=1957 lang=python3
#
# [1957] Delete Characters to Make Fancy String
#
# https://leetcode.com/problems/delete-characters-to-make-fancy-string/description/
#
# algorithms
# Easy (58.89%)
# Likes: 537
# Dislikes: 28
# Total Accepted: 64.3K
# Total Submissions: 98.3K
# Testcase Example: '"leeetcode"'
#
# 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 <= 10^5
# s consists only of lowercase English letters.
#
#
#


# @lc code=start
class Solution:
def makeFancyString(self, s: str) -> str:
result = []

for char in s:
if len(result) >= 2 and result[-1] == char and result[-2] == char:
continue
result.append(char)

return "".join(result)


# @lc code=end

0 comments on commit 6eb5e96

Please sign in to comment.