Skip to main content

Command Palette

Search for a command to run...

Reverse Substrings Between Each Pair of Parentheses

Published
1 min read
Reverse Substrings Between Each Pair of Parentheses
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 that consists of lower case English letters and brackets.

Reverse the strings in each pair of matching parentheses, starting from the innermost one.

Your result should not contain any brackets.

LeetCode Problem - 1190

class Solution {
    // This method reverses the characters in each pair of matching parentheses in the input string
    public String reverseParentheses(String s) {

        StringBuilder resultString = new StringBuilder(s); // Initialize the result string with the input string
        int startIdx = resultString.lastIndexOf("("); // Find the last opening parenthesis
        int endIdx = resultString.indexOf(")", startIdx); // Find the first closing parenthesis after the last opening parenthesis

        // Continue until there are no more opening parentheses
        while (startIdx != -1) {
            // Extract the substring within the parentheses, reverse it, and replace the original substring
            StringBuilder sb = new StringBuilder(resultString.substring(startIdx + 1, endIdx));
            sb.reverse();
            resultString.replace(startIdx, endIdx + 1, sb.toString());
            // Update the indices for the next iteration
            startIdx = resultString.lastIndexOf("(");
            endIdx = resultString.indexOf(")", startIdx);
        }

        return resultString.toString(); // Return the final processed string
    }
}

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.