Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1190. Reverse Substrings Between Each Pair of Parentheses

Medium71.8% Acceptance
StringStack
Asked by:
M
Microsoft
PayPal
ProblemHints (3)Solutions (12)VideosCompanies (2)Notes

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 substring "oc", then "etco", and finally, the whole string.

Constraints:

  • 1 <= s.length <= 2000
  • s only contains lower case English characters and parentheses.
  • It is guaranteed that all parentheses are balanced.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
P

Approach

To solve #1190 Reverse Substrings Between Each Pair of Parentheses, the key challenge is handling nested parentheses while reversing only the characters inside them. A natural way to manage nested structures is by using a stack.

Iterate through the string character by character. When you encounter an opening parenthesis (, push the current accumulated string onto a stack and start building a new substring. When a closing parenthesis ) appears, reverse the current substring and append it to the string stored at the top of the stack. This effectively processes the innermost parentheses first, which matches the required behavior.

An alternative optimized approach precomputes matching parenthesis indices and traverses the string while dynamically changing direction. Both strategies ensure correct handling of deeply nested parentheses.

The stack-based approach runs in O(n) time where n is the string length, with O(n) auxiliary space for the stack and intermediate strings.

Complexity

ApproachTime ComplexitySpace Complexity
Stack-based substring constructionO(n)O(n)
Precomputed parenthesis mapping with directional traversalO(n)O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Find all brackets in the string.

2
Hint 2

Does the order of the reverse matter ?

3
Hint 3

The order does not matter.

Ready to see the solutions?View Solutions

Solutions (12)

Stack-Based Approach

Utilize a stack to handle the nested or paired parentheses efficiently. By pushing characters onto a stack until a closing parenthesis is encountered, then reversing the needed substring, you can leverage the stack's LIFO properties to achieve the desired result.

Time Complexity: O(n).
Space Complexity: O(n) due to the stack usage for storing characters.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5char* reverseParentheses(char* s) {

Explanation

This C solution uses an array-based stack to reverse substrings between parenthesis pairs. It iterates through the string, storing characters inside a stack until a closing parenthesis requires a substring reversal. The reversed substring is then pushed back onto the stack, achieving the desired sequence without parentheses.

Two-Pass String Build Approach

This approach involves separately building the result string in a single pass using an auxiliary data structure to track position swaps. The use of local in-string reversals enables an efficient and clean traversal building mechanism.

Time Complexity: O(n).
Space Complexity: O(n), using additional space for parentheses pair tracking and intermediate char arrays.

CC++JavaPythonC#JavaScript
1





Video Solutions

Watch expert explanations and walkthroughs

Reverse Substrings Between Each Pair of Parentheses - Leetcode 1190 - Python

NeetCodeIO
24:3310,740 views

Asked By Companies

2 companies
M
Microsoft
P
PayPal

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Valid ParenthesesEasy
Longest Valid ParenthesesHard
Simplify PathMedium
Basic CalculatorHard
More similar problems

Related Topics

StringStack

Problem Stats

Acceptance Rate71.8%
DifficultyMedium
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Can this problem be solved without explicitly reversing multiple substrings?

Yes, an optimized approach maps each pair of parentheses and traverses the string while switching traversal direction when a parenthesis is hit. This avoids repeated string reversals and still achieves linear time complexity.

Is Reverse Substrings Between Each Pair of Parentheses asked in coding interviews?

Yes, variations of this problem appear in technical interviews because it tests string manipulation, stack usage, and handling nested structures. Companies often use similar problems to evaluate problem-solving with stacks and parsing logic.

What data structure is best for Reverse Substrings Between Each Pair of Parentheses?

A stack is the most suitable data structure because it naturally supports nested structures like parentheses. It allows you to temporarily store previous substrings and correctly rebuild the result when a closing parenthesis is encountered.

What is the optimal approach for Reverse Substrings Between Each Pair of Parentheses?

The most common optimal approach uses a stack to handle nested parentheses. When encountering a closing parenthesis, the current substring is reversed and combined with the previous string from the stack. This ensures inner parentheses are processed before outer ones.

6
char
stack
[
2000
]
;
7
int
top
=
-
1
;
8
int
n
=
strlen
(
s
)
;
9
10
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
11
if
(
s
[
i
]
==
')'
)
{
12
int
start
=
top
;
13
while
(
stack
[
top
]
!=
'('
)
top
--
;
14
top
--
;
// pop '('
15
for
(
int
k
=
start
;
k
>
top
;
k
--
)
16
stack
[
++
top
]
=
stack
[
k
]
;
17
}
else
{
18
stack
[
++
top
]
=
s
[
i
]
;
19
}
20
}
21
stack
[
top
+
1
]
=
'\0'
;
22
return
strdup
(
stack
)
;
23
}
24
25
int
main
(
)
{
26
char
s
[
]
=
"(u(love)i)"
;
27
char
*
result
=
reverseParentheses
(
s
)
;
28
printf
(
"%s\n"
,
result
)
;
29
free
(
result
)
;
30
return
0
;
31
}
32
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<string.h>
4
5
void
reverse
(
char
*
s
,
int
start
,
int
end
)
{
6
while
(
start
<
end
)
{
7
char
temp
=
s
[
start
]
;
8
s
[
start
]
=
s
[
end
]
;
9
s
[
end
]
=
temp
;
10
start
++
;
11
end
--
;
12
}
13
}
14
15
char
*
reverseParentheses
(
char
*
s
)
{
16
int
len
=
strlen
(
s
)
;
17
int
*
pair
=
malloc
(
len
*
sizeof
(
int
)
)
;
18
int
*
stack
=
malloc
(
len
*
sizeof
(
int
)
)
;
19
int
top
=
-
1
;
20
21
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
22
if
(
s
[
i
]
==
'('
)
{
23
stack
[
++
top
]
=
i
;
24
}
else
if
(
s
[
i
]
==
')'
)
{
25
int
j
=
stack
[
top
--
]
;
26
pair
[
i
]
=
j
;
27
pair
[
j
]
=
i
;
28
}
29
}
30
31
char
*
result
=
malloc
(
len
+
1
)
;
32
int
idx
=
0
,
dir
=
1
;
33
for
(
int
i
=
0
;
i
<
len
;
i
+=
dir
)
{
34
if
(
s
[
i
]
==
'('
||
s
[
i
]
==
')'
)
{
35
i
=
pair
[
i
]
;
36
dir
=
-
dir
;
37
}
else
{
38
result
[
idx
++
]
=
s
[
i
]
;
39
}
40
}
41
result
[
idx
]
=
'\0'
;
42
free
(
pair
)
;
43
free
(
stack
)
;
44
45
return
result
;
46
}
47
48
int
main
(
)
{
49
char
s
[
]
=
"(ed(et(oc))el)"
;
50
char
*
result
=
reverseParentheses
(
s
)
;
51
printf
(
"%s\n"
,
result
)
;
52
free
(
result
)
;
53
return
0
;
54
}
55

Explanation

This C solution divides the task into first creating pair indices for easy traversal and reversal through position swapping, enabling an efficient processing path that aligns with the stack-based idea but applied to index transformations and direct character use.