Skip to content

1190. Reverse Substrings Between Each Pair of Parentheses #221

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 plan:

  1. Use a stack to keep track of the characters and nested parentheses.
  2. Traverse each character in the string.
  3. If you encounter an opening parenthesis '(', push it onto the stack.
  4. If you encounter a closing parenthesis ')', pop from the stack until you reach an opening parenthesis '('. Reverse the substring collected and push it back onto the stack.
  5. Finally, concatenate the stack contents to get the result.

Here's the implementation in PHP: 1190. Reverse Substrings Between Each Pair of Parentheses

<?php
function reverseParentheses($s) {
        $stack = [];

        // Traverse each character in the string
        for ($i = 0; $i < strlen($s); $i++) {
            

Replies: 1 comment 2 replies

Comment options

mah-shamim
Aug 8, 2024
Maintainer Author

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

@mah-shamim
Comment options

mah-shamim Jan 6, 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