Skip to content

1717. Maximum Score From Removing Substrings #1960

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

Here's the step-by-step implementation in PHP:

  1. Determine the order of removal: It is crucial to decide the order of removing "ab" and "ba". If x > y, it is better to remove "ab" first, otherwise, remove "ba" first. This is because removing the more valuable substring first maximizes the score.

  2. Use a stack to process the string: By using a stack, you can efficiently manage the removals. You push characters onto the stack and check the top of the stack to see if you can remove the desired substring.

Here's the implementation in PHP:

<?php

class Solution {

    /**
     * @param String $s
     * @param Integer $x
     * @param Integer $y
     * @return Integer
     */
    function max…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jul 23, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants
Converted from issue

This discussion was converted from issue #23 on July 23, 2025 02:29.