Search Results
7/24/2025, 8:51:29 AM
7/24/2025, 12:34:07 AM
>>106003058
def maximumGain(self, s: str, x: int, y: int) -> int:
def remove_pairs(s, first, second, points):
stack = []
score = 0
for ch in s:
if stack and stack[-1] == first and ch == second:
score += points
stack.pop()
else:
stack.append(ch)
return "".join(stack), score
if x >= y:
# remove "ab" first
s, first_score = remove_pairs(s, "a", "b", x)
_, second_score = remove_pairs(s, "b", "a", y)
else:
# remove "ba" first
s, first_score = remove_pairs(s, "b", "a", y)
_, second_score = remove_pairs(s, "a", "b", x)
return first_score + second_score
def maximumGain(self, s: str, x: int, y: int) -> int:
def remove_pairs(s, first, second, points):
stack = []
score = 0
for ch in s:
if stack and stack[-1] == first and ch == second:
score += points
stack.pop()
else:
stack.append(ch)
return "".join(stack), score
if x >= y:
# remove "ab" first
s, first_score = remove_pairs(s, "a", "b", x)
_, second_score = remove_pairs(s, "b", "a", y)
else:
# remove "ba" first
s, first_score = remove_pairs(s, "b", "a", y)
_, second_score = remove_pairs(s, "a", "b", x)
return first_score + second_score
7/23/2025, 4:41:37 AM
Page 1