1717. Maximum Score From Removing Substrings Medium

@problem@discussion
#String#Stack#Greedy



1/**
2 * [1717] Maximum Score From Removing Substrings
3 *
4 * You are given a string s and two integers x and y. You can perform two types of operations any number of times.
5 * 
6 * 	Remove substring "ab" and gain x points.
7 * 	
8 * 		For example, when removing "ab" from "c<u>ab</u>xbae" it becomes "cxbae".
9 * 	
10 * 	
11 * 	Remove substring "ba" and gain y points.
12 * 	
13 * 		For example, when removing "ba" from "cabx<u>ba</u>e" it becomes "cabxe".
14 * 	
15 * 	
16 * 
17 * Return the maximum points you can gain after applying the above operations on s.
18 *  
19 * Example 1:
20 * 
21 * Input: s = "cdbcbbaaabab", x = 4, y = 5
22 * Output: 19
23 * Explanation:
24 * - Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
25 * - Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
26 * - Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
27 * - Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
28 * Total score = 5 + 4 + 5 + 5 = 19.
29 * Example 2:
30 * 
31 * Input: s = "aabbaaxybbaabb", x = 5, y = 4
32 * Output: 20
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= s.length <= 10^5
38 * 	1 <= x, y <= 10^4
39 * 	s consists of lowercase English letters.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/maximum-score-from-removing-substrings/
45// discuss: https://leetcode.com/problems/maximum-score-from-removing-substrings/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1717() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.