Problem solution · Java

Maximum Score From Removing Substrings

Maximum Score From Removing Substrings: a Java solution using stack-based processing. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Stack-based processing
Source
walkccc LeetCode Solutions
Length
39 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Stack-based processing

For Maximum Score From Removing Substrings, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.

  1. Define what every stack entry represents.
  2. Pop entries once the current item resolves or invalidates them.
  3. Push the current item with only the information later steps need.

Code notes

  • 39 lines of Java from the credited upstream file 1717.java.
  • The implementation keeps its working state in language-native values and containers.
  • 2 loop blocks detected.

Complexity

If each item is pushed and popped at most once, the stack work is linear.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Score From Removing Substrings · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int maximumGain(String s, int x, int y) {    // The assumption that gain("ab") > gain("ba") while removing "ba" first is    // optimal is contradicted. Only "b(ab)a" satisfies the condition of    // preventing two "ba" removals, but after removing "ab", we can still    // remove one "ba", resulting in a higher gain. Thus, removing "ba" first is    // not optimal.    return x > y ? gain(s, "ab", x, "ba", y) : gain(s, "ba", y, "ab", x);  }   // Returns the points gained by first removing sub1 ("ab" | "ba") from s with  // point1, then removing sub2 ("ab" | "ba") from s with point2.  private int gain(final String s, final String sub1, int point1, final String sub2, int point2) {    int points = 0;    Stack<Character> stack1 = new Stack<>();    Stack<Character> stack2 = new Stack<>();     // Remove "sub1" from s with point1 gain.    for (final char c : s.toCharArray())      if (!stack1.isEmpty() && stack1.peek() == sub1.charAt(0) && c == sub1.charAt(1)) {        stack1.pop();        points += point1;      } else {        stack1.push(c);      }     // Remove "sub2" from s with point2 gain.    for (final char c : stack1)      if (!stack2.isEmpty() && stack2.peek() == sub2.charAt(0) && c == sub2.charAt(1)) {        stack2.pop();        points += point2;      } else {        stack2.push(c);      }     return points;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗