Post

1818 Maximum Score From Removing Substrings

1818 Maximum Score From Removing Substrings

Maximum Score From Removing Substrings image

You are given a string s and two integers x and y. You can perform two types of operations any number of times.

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
**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:

1
2
3
4
**Input:** s = "aabbaaxybbaabb", x = 5, y = 4
**Output:** 20

 

Constraints:

1
2
3
1 <= s.length <= 105
1 <= x, y <= 104
s consists of lowercase English letters.
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

impl Solution {
    pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 {
        let mut points = 0;
        let mut chars: Vec<char> = s.chars().collect();

        if x > y {
            points += Self::remove_all(&mut chars, 'a', 'b', x);
            points += Self::remove_all(&mut chars, 'b', 'a', y);
        } else {
            points += Self::remove_all(&mut chars, 'b', 'a', y);
            points += Self::remove_all(&mut chars, 'a', 'b', x);
        }

        points
    }

    fn remove_all(chars: &mut Vec<char>, first: char, second: char, score: i32) -> i32 {
        let mut stack = Vec::new();
        let mut points = 0;

        for &ch in chars.iter() {
            if let Some(&last) = stack.last() {
                if last == first && ch == second {
                    stack.pop();
                    points += score;
                    continue;
                }
            }
            stack.push(ch);
        }

        // Replace original vec with remaining characters for next removal round
        chars.clear();
        chars.extend(stack);
        points
    }
}




This post is licensed under CC BY 4.0 by the author.