aboutsummaryrefslogtreecommitdiff
path: root/make_string_great.py
diff options
context:
space:
mode:
Diffstat (limited to 'make_string_great.py')
-rw-r--r--make_string_great.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/make_string_great.py b/make_string_great.py
new file mode 100644
index 0000000..aa63fa7
--- /dev/null
+++ b/make_string_great.py
@@ -0,0 +1,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"))