1717. Maximum Score From Removing Substrings
Question

Code

class Solution:
def maximumGain(self, s: str, x: int, y: int) -> int:
total_score = 0
high_priority_pair = "ab" if x > y else "ba"
low_priority_pair = "ba" if high_priority_pair == "ab" else "ab"
# First pass: remove high priority pair
string_after_first_pass = self.remove_substring(s, high_priority_pair)
removed_pairs_count = (len(s) - len(string_after_first_pass)) // 2
# Calculate score from first pass
total_score += removed_pairs_count * max(x, y)
# Second pass: remove low priority pair
string_after_second_pass = self.remove_substring(
string_after_first_pass, low_priority_pair
)
removed_pairs_count = (
len(string_after_first_pass) - len(string_after_second_pass)
) // 2
# Calculate score from second pass
total_score += removed_pairs_count * min(x, y)
return total_score
def remove_substring(self, input: str, target_pair: str) -> str:
char_stack = []
# Iterate through each character in the input string
for current_char in input:
# Check if current character forms the target pair with the top of the stack
if (
current_char == target_pair[1]
and char_stack
and char_stack[-1] == target_pair[0]
):
char_stack.pop() # Remove the matching character from the stack
else:
char_stack.append(current_char)
# Reconstruct the remaining string after removing target pairs
return "".join(char_stack)