Advertisement

Reverse Substrings Between Each Pair of Parentheses - LeetCode 1190 Solution

Reverse Substrings Between Each Pair of Parentheses - Complete Solution Guide

Reverse Substrings Between Each Pair of Parentheses is LeetCode problem 1190, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: s = "(abcd)" Output: "dcba" Example 2: Input: s = "(u(love)i)" Output: "iloveu" Explanation: The substring "love" is reversed first, then the whole string is reversed. Example 3: Input: s = "(ed(et(oc))el)" Output: "leetcode" Explanation: First, we reverse the su

Detailed Explanation

The problem requires reversing substrings enclosed within matching parentheses in a given string. The reversals should be performed from the innermost parentheses outwards. The final output string should not contain any parentheses.

Solution Approach

The solution utilizes a stack to simulate the nested structure of parentheses. When an opening parenthesis '(' is encountered, a new list (or vector/ArrayList) is pushed onto the stack. When a closing parenthesis ')' is encountered, the top list is popped, reversed, and then appended to the list at the top of the stack. Characters other than parentheses are added to the list at the top of the stack. This process effectively reverses the substrings enclosed by each pair of matching parentheses, starting from the innermost ones.

Step-by-Step Algorithm

  1. Step 1: Initialize a stack with an empty list (or vector/ArrayList) as the initial element.
  2. Step 2: Iterate through the input string character by character.
  3. Step 3: If the character is an opening parenthesis '(', push a new empty list onto the stack.
  4. Step 4: If the character is a closing parenthesis ')', pop the top list from the stack, reverse it, and then extend the list at the top of the stack with the reversed list.
  5. Step 5: If the character is a lowercase English letter, append it to the list at the top of the stack.
  6. Step 6: After processing all characters, the remaining list at the bottom of the stack contains the final reversed string. Join the characters in the list to form a string and return it.

Key Insights

  • Insight 1: The problem can be effectively solved using a stack data structure to keep track of the nesting of parentheses.
  • Insight 2: When encountering a closing parenthesis, the characters accumulated since the last opening parenthesis need to be reversed.
  • Insight 3: Each stack level represents a level of nested parentheses.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Stack.

Companies

Asked at: Agoda.