Skip to main content

Command Palette

Search for a command to run...

Maximum Score From Removing Substrings

Published
2 min read
Maximum Score From Removing Substrings
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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 "cabxbae" it becomes "cxbae".
  • Remove substring "ba" and gain y points.

    • For example, when removing "ba" from "cabxbae" it becomes "cabxe".

Return the maximum points you can gain after applying the above operations on s.

LeetCode Problem - 1717

class Solution {
    // This method calculates the maximum gain from removing the patterns "ab" and "ba" from the string s
    public int maximumGain(String s, int x, int y) {
        int score = 0;
        // Prioritize the pattern with the higher points
        if (x > y) {
            score += process(s, "ab", x, "ba", y);
        } else {
            score += process(s, "ba", y, "ab", x);
        }
        return score;
    }

    // This method processes the string to remove the specified patterns and calculate the score
    private int process(String s, String firstPattern, int firstPoints, String secondPattern, int secondPoints) {
        int score = 0;
        StringBuilder sb = new StringBuilder();

        // Remove the first pattern and accumulate points
        for (char c : s.toCharArray()) {
            sb.append(c);
            int len = sb.length();
            if (len >= 2 && sb.substring(len - 2).equals(firstPattern)) {
                sb.setLength(len - 2); // Remove the pattern from the string
                score += firstPoints; // Add points for the pattern
            }
        }

        // Process the remaining string for the second pattern
        s = sb.toString();
        sb.setLength(0);

        // Remove the second pattern and accumulate points
        for (char c : s.toCharArray()) {
            sb.append(c);
            int len = sb.length();
            if (len >= 2 && sb.substring(len - 2).equals(secondPattern)) {
                sb.setLength(len - 2); // Remove the pattern from the string
                score += secondPoints; // Add points for the pattern
            }
        }

        return score;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.