Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26a1071

Browse files
feat: solve NO.1717
1 parent 93d20bf commit 26a1071

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# 1717. Maximum Score From Removing Substrings
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String, Stack, Greedy.
5+
- Similar Questions: Count Words Obtained After Adding a Letter.
6+
7+
## Problem
8+
9+
You are given a string `s` and two integers `x` and `y`. You can perform two types of operations any number of times.
10+
11+
12+
Remove substring `"ab"` and gain `x` points.
13+
14+
15+
16+
- For example, when removing `"ab"` from `"cabxbae"` it becomes `"cxbae"`.
17+
18+
19+
Remove substring `"ba"` and gain `y` points.
20+
21+
22+
- For example, when removing `"ba"` from `"cabxbae"` it becomes `"cabxe"`.
23+
24+
25+
26+
27+
Return **the maximum points you can gain after applying the above operations on** `s`.
28+
29+
30+
Example 1:
31+
32+
```
33+
Input: s = "cdbcbbaaabab", x = 4, y = 5
34+
Output: 19
35+
Explanation:
36+
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
37+
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
38+
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
39+
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
40+
Total score = 5 +たす 4 +たす 5 +たす 5 = 19.
41+
```
42+
43+
Example 2:
44+
45+
```
46+
Input: s = "aabbaaxybbaabb", x = 5, y = 4
47+
Output: 20
48+
```
49+
50+
51+
**Constraints:**
52+
53+
54+
55+
- `1 <= s.length <= 105`
56+
57+
- `1 <= x, y <= 104`
58+
59+
- `s` consists of lowercase English letters.
60+
61+
62+
63+
## Solution 1
64+
65+
```javascript
66+
/**
67+
* @param {string} s
68+
* @param {number} x
69+
* @param {number} y
70+
* @return {number}
71+
*/
72+
var maximumGain = function(s, x, y) {
73+
var stack = [];
74+
var score = 0;
75+
var a = x > y ? 'ab' : 'ba';
76+
var b = x > y ? 'ba' : 'ab';
77+
var ax = x > y ? x : y;
78+
var bx = x > y ? y : x;
79+
var score = 0;
80+
for (var i = 0; i < s.length; i++) {
81+
if (stack[stack.length - 1] === a[0] && s[i] === a[1]) {
82+
stack.pop();
83+
score += ax;
84+
} else {
85+
stack.push(s[i]);
86+
}
87+
}
88+
s = stack.join('');
89+
stack = [];
90+
for (var i = 0; i < s.length; i++) {
91+
if (stack[stack.length - 1] === b[0] && s[i] === b[1]) {
92+
stack.pop();
93+
score += bx;
94+
} else {
95+
stack.push(s[i]);
96+
}
97+
}
98+
return score;
99+
};
100+
```
101+
102+
**Explain:**
103+
104+
nope.
105+
106+
**Complexity:**
107+
108+
* Time complexity : O(n).
109+
* Space complexity : O(n).
110+
111+
## Solution 2
112+
113+
```javascript
114+
/**
115+
* @param {string} s
116+
* @param {number} x
117+
* @param {number} y
118+
* @return {number}
119+
*/
120+
var maximumGain = function(s, x, y) {
121+
if (y > x) {
122+
return maximumGain(s.split('').reverse(), y, x);
123+
}
124+
var aCount = 0;
125+
var bCount = 0;
126+
var score = 0;
127+
for (var i = 0; i <= s.length; i++) {
128+
if (s[i] === 'a') {
129+
aCount += 1;
130+
} else if (s[i] === 'b') {
131+
if (aCount > 0) {
132+
aCount -= 1;
133+
score += x;
134+
} else {
135+
bCount += 1;
136+
}
137+
} else {
138+
score += Math.min(aCount, bCount) * y;
139+
aCount = 0;
140+
bCount = 0;
141+
}
142+
}
143+
return score;
144+
};
145+
```
146+
147+
**Explain:**
148+
149+
nope.
150+
151+
**Complexity:**
152+
153+
* Time complexity : O(n).
154+
* Space complexity : O(1).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /