blob: 4dd8e916cb446f5612b034b4c2630355ba6d0bdf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# 1957. Delete Characters to Make Fancy String
"""
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
"""
class Solution(object):
def makeFancyString(self, s):
"""
:type s: str
:rtype: str
"""
cnt, ans = 0, []
for i, j in enumerate(s):
if i > 0 and j == s[i - 1]:
cnt += 1
else:
cnt = 1
if cnt < 3:
ans.append(j)
return "".join(ans)
if __name__ == "__main__":
obj = Solution()
print(obj.makeFancyString(s="leeetcode"))
print(obj.makeFancyString(s="aaabaaaa"))
print(obj.makeFancyString(s="aab"))
|