Adjacent Elimination¶
Table of Contents¶
- 2696. Minimum String Length After Removing Substrings (Easy)
- 1047. Remove All Adjacent Duplicates In String (Easy)
- 1544. Make The String Great (Easy)
- 1003. Check If Word Is Valid After Substitutions (Medium)
- 2216. Minimum Deletions to Make Array Beautiful (Medium)
- 1209. Remove All Adjacent Duplicates in String II (Medium)
- 2211. Count Collisions on a Road (Medium)
- 735. Asteroid Collision (Medium)
- 1717. Maximum Score From Removing Substrings (Medium)
- 2197. Replace Non-Coprime Numbers in Array (Hard)
- 2751. Robot Collisions (Hard)
2696. Minimum String Length After Removing Substrings¶
1047. Remove All Adjacent Duplicates In String¶
1544. Make The String Great¶
"""
- Remove all adjacent characters that are the same and have different cases.
- Steps for the string `leEeetcode`:
| char | action | stack |
| ---- | ------ | ---------- |
| l | push | "l" |
| e | push | "le" |
| E | pop | "l" |
| e | push | "le" |
| e | push | "lee" |
| t | push | "leet" |
| c | push | "leetc" |
| o | push | "leetco" |
| d | push | "leetcod" |
| e | push | "leetcode" |
"""
# Stack
def makeGood(s: str) -> str:
stack = []
for i in range(len(s)):
if stack and stack[-1] == s[i].swapcase():
stack.pop()
else:
stack.append(s[i])
return "".join(stack)
print(makeGood("leEeetcode")) # "leetcode"