Skip to content

Commit 18da053

Browse files
committed
New Problem Solution -"Maximum Score From Removing Substrings"
1 parent 3f87c38 commit 18da053

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ LeetCode
6161
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
6262
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy|
6363
|1725|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/) | [C++](./algorithms/cpp/numberOfRectanglesThatCanFormTheLargestSquare/NumberOfRectanglesThatCanFormTheLargestSquare.cpp)|Easy|
64+
|1717|[Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/) | [C++](./algorithms/cpp/maximumScoreFromRemovingSubstrings/MaximumScoreFromRemovingSubstrings.cpp)|Medium|
6465
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/) | [C++](./algorithms/cpp/calculateMoneyInLeetcodeBank/CalculateMoneyInLeetcodeBank.cpp)|Easy|
6566
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/) | [C++](./algorithms/cpp/lexicographicallySmallestStringAfterApplyingOperations/LexicographicallySmallestStringAfterApplyingOperations.cpp)|Medium|
6667
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [C++](./algorithms/cpp/largestSubstringBetweenTwoEqualCharacters/LargestSubstringBetweenTwoEqualCharacters.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Source : https://leetcode.com/problems/maximum-score-from-removing-substrings/
2+
// Author : Hao Chen
3+
// Date : 2021-03-28
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given a string s and two integers x and y. You can perform two types of operations any
8+
* number of times.
9+
*
10+
* Remove substring "ab" and gain x points.
11+
*
12+
* For example, when removing "ab" from "cabxbae" it becomes "cxbae".
13+
*
14+
* Remove substring "ba" and gain y points.
15+
*
16+
* For example, when removing "ba" from "cabxbae" it becomes "cabxe".
17+
*
18+
* Return the maximum points you can gain after applying the above operations on s.
19+
*
20+
* Example 1:
21+
*
22+
* Input: s = "cdbcbbaaabab", x = 4, y = 5
23+
* Output: 19
24+
* Explanation:
25+
* - Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the
26+
* score.
27+
* - Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the
28+
* score.
29+
* - Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
30+
* - Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
31+
* Total score = 5 + 4 + 5 + 5 = 19.
32+
*
33+
* Example 2:
34+
*
35+
* Input: s = "aabbaaxybbaabb", x = 5, y = 4
36+
* Output: 20
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= s.length <= 10^5
41+
* 1 <= x, y <= 10^4
42+
* s consists of lowercase English letters.
43+
******************************************************************************************************/
44+
45+
class Solution {
46+
public:
47+
int maximumGain(string s, int x, int y) {
48+
char key[] ="ab";
49+
if (y > x) { key[0] = 'b'; key[1]='a';}
50+
51+
int high = max(x,y);
52+
int low = min(x,y);
53+
54+
//greedy for high score
55+
int score = 0;
56+
stack<char> left_stack;
57+
for (int i=0; i<s.size(); i++) {
58+
char c = s[i];
59+
if ( left_stack.empty() || //stack is empty, just push directly
60+
( c != key[0] && c != key[1] ) ) { // not the score char, just tpush cirectory
61+
left_stack.push(c);
62+
continue;
63+
}
64+
65+
// if we meet the high score pattern
66+
if ( c == key[1] && left_stack.top() == key[0]){
67+
//cout << key << endl;
68+
left_stack.pop();
69+
score += high;
70+
continue;
71+
}
72+
left_stack.push(c);
73+
}
74+
75+
//process the low score
76+
stack<char> right_stack;
77+
while(!left_stack.empty()) {
78+
char c = left_stack.top(); left_stack.pop();
79+
if (right_stack.empty() || c != key[0] && c != key[1]) {
80+
right_stack.push(c);
81+
continue;
82+
}
83+
// if we meet the low score pattern
84+
if ( c == key[1] && right_stack.top() == key[0]){
85+
right_stack.pop();
86+
score += low;
87+
continue;
88+
}
89+
90+
right_stack.push(c);
91+
}
92+
return score;
93+
}
94+
};

0 commit comments

Comments
 (0)