forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumScoreFromRemovingSubstrings.cpp
94 lines (87 loc) · 3.02 KB
/
MaximumScoreFromRemovingSubstrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Source : https://leetcode.com/problems/maximum-score-from-removing-substrings/
// Author : Hao Chen
// Date : 2021-03-28
/*****************************************************************************************************
*
* You are given a string s and two integers x and y. You can perform two types of operations any
* number of times.
*
* Remove substring "ab" and gain x points.
*
* For example, when removing "ab" from "cabxbae" it becomes "cxbae".
*
* Remove substring "ba" and gain y points.
*
* For example, when removing "ba" from "cabxbae" it becomes "cabxe".
*
* Return the maximum points you can gain after applying the above operations on s.
*
* Example 1:
*
* Input: s = "cdbcbbaaabab", x = 4, y = 5
* Output: 19
* Explanation:
* - Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the
* score.
* - Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the
* score.
* - Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
* - Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
* Total score = 5 + 4 + 5 + 5 = 19.
*
* Example 2:
*
* Input: s = "aabbaaxybbaabb", x = 5, y = 4
* Output: 20
*
* Constraints:
*
* 1 <= s.length <= 10^5
* 1 <= x, y <= 10^4
* s consists of lowercase English letters.
******************************************************************************************************/
class Solution {
public:
int maximumGain(string s, int x, int y) {
char key[] ="ab";
if (y > x) { key[0] = 'b'; key[1]='a';}
int high = max(x,y);
int low = min(x,y);
//greedy for high score
int score = 0;
stack<char> left_stack;
for (int i=0; i<s.size(); i++) {
char c = s[i];
if ( left_stack.empty() || //stack is empty, just push directly
( c != key[0] && c != key[1] ) ) { // not the score char, just tpush cirectory
left_stack.push(c);
continue;
}
// if we meet the high score pattern
if ( c == key[1] && left_stack.top() == key[0]){
//cout << key << endl;
left_stack.pop();
score += high;
continue;
}
left_stack.push(c);
}
//process the low score
stack<char> right_stack;
while(!left_stack.empty()) {
char c = left_stack.top(); left_stack.pop();
if (right_stack.empty() || c != key[0] && c != key[1]) {
right_stack.push(c);
continue;
}
// if we meet the low score pattern
if ( c == key[1] && right_stack.top() == key[0]){
right_stack.pop();
score += low;
continue;
}
right_stack.push(c);
}
return score;
}
};