Advertisement

Maximum Score From Removing Substrings - LeetCode 1717 Solution

Maximum Score From Removing Substrings - Complete Solution Guide

Maximum Score From Removing Substrings is LeetCode problem 1717, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 "c ab xbae" it becomes "cxbae" . Remove substring "ba" and gain y points. For example, when removing "ba" from "cabx ba e" 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

Detailed Explanation

The problem asks us to find the maximum score that can be obtained by repeatedly removing either the substring "ab" with score x or the substring "ba" with score y from a given string s. We can perform these removals any number of times and in any order. The goal is to maximize the total score from these removals.

Solution Approach

The solution employs a greedy strategy combined with a stack data structure. First, it determines whether x > y or y > x and prioritizes removal of the substring corresponding to the larger score. It iterates through the input string and uses a stack to keep track of characters. If the top of the stack combined with the current character forms either 'ab' or 'ba' (depending on which is being prioritized), the pair is removed (the top character is popped from the stack, and the current character is skipped), and the corresponding score is added. If not, the current character is pushed onto the stack. This process is repeated for both 'ab' and 'ba', regardless of the prioritization.

Step-by-Step Algorithm

  1. Step 1: Define a helper function `remove_and_score(text, pair, points)` that takes a string, a substring pair ("ab" or "ba"), and the score for removing that pair as input.
  2. Step 2: Inside `remove_and_score`, initialize an empty stack and a `current_score` to 0.
  3. Step 3: Iterate through the input `text` character by character.
  4. Step 4: If the stack is not empty and the top of the stack combined with the current character forms the target `pair`, pop the top of the stack and increment `current_score` by `points`.
  5. Step 5: Otherwise, push the current character onto the stack.
  6. Step 6: After processing the entire `text`, return the remaining string (represented by the characters left in the stack) and the `current_score`.
  7. Step 7: In the `maximumGain` function, compare x and y to determine which substring to prioritize.
  8. Step 8: Call `remove_and_score` with the prioritized substring and corresponding score, then call it again with the remaining string and the other substring and score.
  9. Step 9: Sum the scores from both calls to `remove_and_score` and return the final score.

Key Insights

  • Insight 1: The order of removals matters because removing one substring might expose other substrings that can then be removed.
  • Insight 2: A greedy approach of prioritizing removals based on the higher score (x or y) can lead to the optimal solution.
  • Insight 3: Using a stack data structure can efficiently identify and remove the targeted substrings "ab" and "ba".

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Stack, Greedy.

Companies

Asked at: Swiggy.