Skip to content

Commit a73e0a9

Browse files
committed
Sync LeetCode submission Runtime - 191 ms (81.29%), Memory - 19.4 MB (50.97%)
1 parent 50d6397 commit a73e0a9

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
2+
3+
<ul>
4+
<li>Remove substring <code>&quot;ab&quot;</code> and gain <code>x</code> points.
5+
6+
<ul>
7+
<li>For example, when removing <code>&quot;ab&quot;</code> from <code>&quot;c<u>ab</u>xbae&quot;</code> it becomes <code>&quot;cxbae&quot;</code>.</li>
8+
</ul>
9+
</li>
10+
<li>Remove substring <code>&quot;ba&quot;</code> and gain <code>y</code> points.
11+
<ul>
12+
<li>For example, when removing <code>&quot;ba&quot;</code> from <code>&quot;cabx<u>ba</u>e&quot;</code> it becomes <code>&quot;cabxe&quot;</code>.</li>
13+
</ul>
14+
</li>
15+
</ul>
16+
17+
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> s = &quot;cdbcbbaaabab&quot;, x = 4, y = 5
24+
<strong>Output:</strong> 19
25+
<strong>Explanation:</strong>
26+
- Remove the &quot;ba&quot; underlined in &quot;cdbcbbaaa<u>ba</u>b&quot;. Now, s = &quot;cdbcbbaaab&quot; and 5 points are added to the score.
27+
- Remove the &quot;ab&quot; underlined in &quot;cdbcbbaa<u>ab</u>&quot;. Now, s = &quot;cdbcbbaa&quot; and 4 points are added to the score.
28+
- Remove the &quot;ba&quot; underlined in &quot;cdbcb<u>ba</u>a&quot;. Now, s = &quot;cdbcba&quot; and 5 points are added to the score.
29+
- Remove the &quot;ba&quot; underlined in &quot;cdbc<u>ba</u>&quot;. Now, s = &quot;cdbc&quot; and 5 points are added to the score.
30+
Total score = 5 + 4 + 5 + 5 = 19.</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> s = &quot;aabbaaxybbaabb&quot;, x = 5, y = 4
36+
<strong>Output:</strong> 20
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
44+
<li><code>1 &lt;= x, y &lt;= 10<sup>4</sup></code></li>
45+
<li><code>s</code> consists of lowercase English letters.</li>
46+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Approach 1: Greedy Way (Stack)
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def maximumGain(self, s: str, x: int, y: int) -> int:
8+
total_score = 0
9+
high_priority_pair = 'ab' if x > y else 'ba'
10+
low_priority_pair = 'ba' if high_priority_pair == 'ab' else 'ab'
11+
12+
# First pass: Remove high_priority_pair
13+
string_after_first_pass = self.remove_substring(s, high_priority_pair)
14+
removed_pairs_count = (len(s) - len(string_after_first_pass)) // 2
15+
16+
total_score += removed_pairs_count * max(x, y)
17+
18+
# Second pass: Remove low_priority_pair
19+
string_after_second_pass = self.remove_substring(string_after_first_pass, low_priority_pair)
20+
removed_pairs_count = (len(string_after_first_pass) - len(string_after_second_pass)) // 2
21+
22+
total_score += removed_pairs_count * min(x, y)
23+
24+
return total_score
25+
26+
def remove_substring(self, input, target_pair):
27+
char_stack = []
28+
29+
for curr_char in input:
30+
if(
31+
curr_char == target_pair[1]
32+
and char_stack
33+
and char_stack[-1] == target_pair[0]
34+
):
35+
char_stack.pop()
36+
else:
37+
char_stack.append(curr_char)
38+
39+
return ''.join(char_stack)
40+
41+

0 commit comments

Comments
 (0)