Maximum Score From Removing Substrings
Medium · String, Stack, Greedy
You are given a string s and two integers x and y. You can perform two types of operations any number of times.
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
- 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 <= 105
- 1 <= x, y <= 104
- s consists of lowercase English letters.
Starter code (python)
class Solution(object):
def maximumGain(self, s, x, y):
"""
:type s: str
:type x: int
:type y: int
:rtype: int
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Container With Most Water (Medium)
Previous: Checking Existence of Edge Length Limited Paths · Next: Construct the Lexicographically Largest Valid Sequence · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.