aboutsummaryrefslogtreecommitdiff
path: root/make_string_great.py
blob: aa63fa73f96355ca8f0c6d27d6c5c58d3af07495 (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
33
34
35
36
37
38
# 1544. make the string great

"""
given a string 's' of lower and upper case english letters. a good string is
a string which doesn't have two adjacent characters 's[i]' and 's[i + 1]'
where 0 <= i <= s.len - 2. s[i] is a lower case letter and s[i + 1] is the
same letter but in upper case, or vice versa. to make the string good, you
can choose two adjacent characters that make the string bad and remove them.
you can keep doing this until the string becomes good. return the string
after making it good. the answer is guaranteed to be unique under the given
constraints. notice that an empty string is also good.
"""


class Solution(object):
    def makeGood(self, s):
        """
        :type s: str
        :rtype: str
        """
        ans = []
        for c in s:
            if not ans:
                ans.append(c)
            elif ans[-1].isupper() and ans[-1].lower() == c:
                ans.pop()
            elif ans[-1].islower() and ans[-1].upper() == c:
                ans.pop()
            else:
                ans.append(c)
        return "".join(ans)


if __name__ == "__main__":
    obj = Solution()
    print(obj.makeGood("leEeetcode"))
    print(obj.makeGood("abBAcC"))
    print(obj.makeGood("s"))