-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (77 loc) · 2.53 KB
/
main.go
File metadata and controls
86 lines (77 loc) · 2.53 KB
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
// Source: https://leetcode.com/problems/maximum-score-from-removing-substrings
// Title: Maximum Score From Removing Substrings
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// Use Greedy + Stack
//
// WLOG, assume x > y.
// We push letters into stack one by one, and erase immediately if the top letters are `ab`.
// After pushing all letters, we erase `ba`.
func maximumGain(s string, x int, y int) int {
n := len(s)
ab := []rune{'a', 'b'}
ba := []rune{'b', 'a'}
// WLOG assume x > y
if x < y {
x, y = y, x
ab, ba = ba, ab
}
ans := 0
stack1 := make([]rune, 0, n)
for _, ch := range s {
stack1 = append(stack1, ch)
if len(stack1) >= 2 && stack1[len(stack1)-2] == ab[0] && stack1[len(stack1)-1] == ab[1] {
ans += x
stack1 = stack1[:len(stack1)-2]
}
}
stack2 := make([]rune, 0, n)
for _, ch := range stack1 {
stack2 = append(stack2, ch)
if len(stack2) >= 2 && stack2[len(stack2)-2] == ba[0] && stack2[len(stack2)-1] == ba[1] {
ans += y
stack2 = stack2[:len(stack2)-2]
}
}
return ans
}