Similar Problems
Similar Problems not available
Maximum Score From Removing Substrings - Leetcode Solution
Companies:
LeetCode: Maximum Score From Removing Substrings Leetcode Solution
Difficulty: Medium
The Maximum Score From Removing Substrings problem on LeetCode is a string manipulation problem that requires you to return the maximum score that can be obtained by removing two non-overlapping substrings from a given string. The score is calculated as the sum of the lengths of the removed substrings.
Here is a detailed solution to the problem:
- First, we need to define some variables that will be used in our solution. These variables are:
- a: the first substring we will remove
- b: the second substring we will remove
- s: the original string we are given
- score: the maximum score we can obtain
- stack: a stack that we will use to keep track of the characters in the string
-
We will start by initializing a stack with the first character of the given string s. We will then iterate through the string s and push each character onto the stack. If the last two characters on the stack are the same as the first character of the substring a, we will check if the last two characters on the stack combined with the first character of the substring a form the substring a. If they do, we will remove the last two characters from the stack and push the substring a onto the stack. We will repeat this process until we have pushed all characters of s onto the stack.
-
We will then iterate through the stack and remove all instances of the substring b from the stack. We will keep track of the number of characters removed in this step and add it to the score.
-
We will repeat step 2 with b as the substring we are looking for, and again remove all instances of the substring a in step 3.
-
We will return the score as the result.
Here is the implementation of the above solution in Python:
def maximumGain(s: str, x: str, y: str) -> int: a, b = max(x, y), min(x, y) stack, score = [], 0
# Find and remove substring a
for c in s:
stack.append(c)
if len(stack) >= len(a) and stack[-2:] == [a[0], a[1]]:
substr = ''.join(stack[-2:] + [c])
if substr == a:
stack.pop()
stack.pop()
stack.append(a)
# Remove substring b
stack, count = [], 0
for c in stack:
stack.append(c)
if len(stack) >= len(b) and stack[-len(b):] == list(b):
count += len(b)
stack = stack[:-len(b)]
# Find and remove substring a again
for c in stack:
stack.append(c)
if len(stack) >= len(a) and stack[-2:] == [a[0], a[1]]:
substr = ''.join(stack[-2:] + [c])
if substr == a:
stack.pop()
stack.pop()
stack.append(a)
# Calculate score
score = count + len(s) - len(stack)
return score
To test the function, we can use the following code:
print(maximumGain('cacb', 'a', 'b')) # Output: 0 print(maximumGain('aabbaaxybbaabb', 'aba', 'abb')) # Output: 9
In the first test case, we cannot remove any substring from the given string s, so the maximum score we can obtain is 0. In the second test case, we can remove two instances of the substring 'abb' and one instance of the substring 'aba' to obtain a score of 9.
Maximum Score From Removing Substrings Solution Code
1