From ea7b392996dc3799be46379040f025a988e8d6c3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 31 Oct 2024 18:12:11 -0600 Subject: 1957. delete characters to make fancy string --- delete_char_fancy_string.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 delete_char_fancy_string.py (limited to 'delete_char_fancy_string.py') diff --git a/delete_char_fancy_string.py b/delete_char_fancy_string.py new file mode 100644 index 0000000..4dd8e91 --- /dev/null +++ b/delete_char_fancy_string.py @@ -0,0 +1,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")) -- cgit v1.2.3