From dd5a2f2a38f9ed093161cbbf6ceb16cffd4b2300 Mon Sep 17 00:00:00 2001 From: chayan das Date: Wed, 23 Jul 2025 21:15:37 +0530 Subject: [PATCH] Create 1695. Maximum Erasure Value --- 1695. Maximum Erasure Value | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1695. Maximum Erasure Value diff --git a/1695. Maximum Erasure Value b/1695. Maximum Erasure Value new file mode 100644 index 0000000..7ed8ec6 --- /dev/null +++ b/1695. Maximum Erasure Value @@ -0,0 +1,37 @@ +class Solution { +public: + long long maximumGain(string s, int x, int y) { + long long total = 0; + + if (x >= y) { + auto [afterFirst, gain1] = removePattern(s, 'a', 'b', x); + total += gain1; + auto [_, gain2] = removePattern(afterFirst, 'b', 'a', y); + total += gain2; + } else { + auto [afterFirst, gain1] = removePattern(s, 'b', 'a', y); + total += gain1; + auto [_, gain2] = removePattern(afterFirst, 'a', 'b', x); + total += gain2; + } + + return total; + } + +private: + pair removePattern(const string& s, char first, char second, int value) { + string result; + long long score = 0; + + for (char ch : s) { + if (!result.empty() && result.back() == first && ch == second) { + result.pop_back(); + score += value; + } else { + result.push_back(ch); + } + } + + return {result, score}; + } +};