|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +CREATED AT: 2022年11月08日 |
| 4 | + |
| 5 | +URL: https://leetcode.com/problems/make-the-string-great/ |
| 6 | + |
| 7 | +GITHUB: https://github.com/Jiezhi/myleetcode |
| 8 | + |
| 9 | +FileName: 1544-MakeTheStringGreat |
| 10 | + |
| 11 | +Difficulty: Easy |
| 12 | + |
| 13 | +Desc: |
| 14 | + |
| 15 | +Tag: |
| 16 | + |
| 17 | +See: |
| 18 | + |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def makeGood(self, s: str) -> str: |
| 24 | + """ |
| 25 | + Runtime: 91 ms, faster than 8.21% |
| 26 | + Memory Usage: 13.9 MB, less than 15.06% |
| 27 | + 1 <= s.length <= 100 |
| 28 | + s contains only lower and upper case English letters. |
| 29 | + """ |
| 30 | + s = list(s) |
| 31 | + cur, pre = len(s), 0 |
| 32 | + while cur != pre: |
| 33 | + pre = cur |
| 34 | + i = 0 |
| 35 | + while i < len(s) - 1: |
| 36 | + if s[i] != s[i + 1] and s[i].lower() == s[i + 1].lower(): |
| 37 | + s.pop(i) |
| 38 | + s.pop(i) |
| 39 | + else: |
| 40 | + i += 1 |
| 41 | + cur = len(s) |
| 42 | + return ''.join(s) |
| 43 | + |
| 44 | + |
| 45 | +def test(): |
| 46 | + assert Solution().makeGood(s="leEeetcode") == "leetcode" |
| 47 | + assert Solution().makeGood(s="abBAcC") == "" |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + test() |
0 commit comments