Skip to content

Commit dd5a2f2

Browse files
authored
Create 1695. Maximum Erasure Value
1 parent 897e161 commit dd5a2f2

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

1695. Maximum Erasure Value

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
long long maximumGain(string s, int x, int y) {
4+
long long total = 0;
5+
6+
if (x >= y) {
7+
auto [afterFirst, gain1] = removePattern(s, 'a', 'b', x);
8+
total += gain1;
9+
auto [_, gain2] = removePattern(afterFirst, 'b', 'a', y);
10+
total += gain2;
11+
} else {
12+
auto [afterFirst, gain1] = removePattern(s, 'b', 'a', y);
13+
total += gain1;
14+
auto [_, gain2] = removePattern(afterFirst, 'a', 'b', x);
15+
total += gain2;
16+
}
17+
18+
return total;
19+
}
20+
21+
private:
22+
pair<string, long long> removePattern(const string& s, char first, char second, int value) {
23+
string result;
24+
long long score = 0;
25+
26+
for (char ch : s) {
27+
if (!result.empty() && result.back() == first && ch == second) {
28+
result.pop_back();
29+
score += value;
30+
} else {
31+
result.push_back(ch);
32+
}
33+
}
34+
35+
return {result, score};
36+
}
37+
};

0 commit comments

Comments
 (0)