diff options
author | Jack Sangdahl <[email protected]> | 2024-10-31 18:12:11 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-31 18:12:11 -0600 |
commit | ea7b392996dc3799be46379040f025a988e8d6c3 (patch) | |
tree | 9aad99d97e43f2aaa39771195c9727917afcf9a8 /delete_char_fancy_string.py | |
parent | 45a8af12d6d2f60e1efdba06d853aaff9055193f (diff) | |
download | leetcode-ea7b392996dc3799be46379040f025a988e8d6c3.tar.gz leetcode-ea7b392996dc3799be46379040f025a988e8d6c3.zip |
1957. delete characters to make fancy string
Diffstat (limited to 'delete_char_fancy_string.py')
-rw-r--r-- | delete_char_fancy_string.py | 32 |
1 files changed, 32 insertions, 0 deletions
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")) |