Magicsheet logo

Maximum Score From Removing Substrings

Medium
58%
Updated 6/1/2025

Maximum Score From Removing Substrings

1. What is this problem about?

The Maximum Score From Removing Substrings interview question is a string optimization problem. You are given a string s and two specific substrings, "ab" and "ba", each with an associated point value. You can remove these substrings from s in any order. When a substring is removed, the remaining parts of the string are joined together, which might create new "ab" or "ba" patterns.

The goal is to maximize the total score by strategically choosing which substring to prioritize and remove first.

2. Why is this asked in interviews?

This Maximum Score From Removing Substrings coding problem is a favorite at companies like Microsoft and Meta. It tests your ability to identify a greedy strategy—specifically, that you should always prioritize removing the substring with the higher point value first. It also evaluates your proficiency in using a Stack to efficiently remove characters from a string in O(N)O(N) time, avoiding the O(N2)O(N^2) overhead of repeated string slicing.

3. Algorithmic pattern used

The String, Stack, Greedy interview pattern is the optimal way to solve this.

  1. Determine which substring ("ab" or "ba") has more points. Let's say "ab" is worth more.
  2. Use a stack to perform a first pass through the string, removing all occurrences of "ab".
  3. After the first pass, use the resulting stack (or a new stack) to perform a second pass, removing all occurrences of "ba".
  4. Sum the points gained from both passes.

4. Example explanation

String: "aabbaa", Points: "ab"=5, "ba"=10. Since "ba" is worth more, prioritize "ba".

  1. First pass (for "ba"):
    • 'a', 'a', 'b', 'b', 'a', 'a'
    • Stack: ['a', 'a', 'b'] -> next is 'b'. No match.
    • Stack: ['a', 'a', 'b', 'b'] -> next is 'a'. Match "ba"! Pop 'b'. Stack: ['a', 'a', 'b']. Points = 10.
    • Stack: ['a', 'a', 'b'] -> next is 'a'. Match "ba"! Pop 'b'. Stack: ['a', 'a']. Points = 20. Result after first pass: "aa".
  2. Second pass (for "ab"):
    • "aa" has no "ab". Points = 0. Total Score = 20.

5. Common mistakes candidates make

In the Maximum Score From Removing Substrings coding problem, a common error is not using a stack, leading to a slow O(N2)O(N^2) solution that fails on large strings. Another mistake is not realizing the greedy property—thinking that removing a lower-value substring first might somehow "unlock" more high-value substrings later. However, since "ab" and "ba" share the same characters, removing the more valuable one as much as possible is always optimal.

6. Interview preparation tip

For string problems involving "removals" and "re-joining," always consider using a stack. A stack allows you to compare the current character with the most recent "unmatched" character, which is exactly what you need for pattern removal. Practice implementing this pattern efficiently without creating multiple intermediate string objects.

Similar Questions