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

921. Minimum Add to Make Parentheses Valid

Medium74.7% Acceptance
StringStackGreedy
Asked by:
F
Facebook
ProblemSolutions (12)VideosCompanies (4)Notes

Problem Statement

A parentheses string is valid if and only if:

  • It is the empty string,
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.

  • For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".

Return the minimum number of moves required to make s valid.

Example 1:

Input: s = "())"
Output: 1

Example 2:

Input: s = "((("
Output: 3

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either '(' or ')'.
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
M
Microsoft
A
Amazon
T
Twitter

Approach

In #921 Minimum Add to Make Parentheses Valid, the goal is to determine the minimum number of parentheses that must be added to make a given string valid. A valid parentheses string requires every opening bracket ( to have a corresponding closing bracket ) in the correct order.

A common and efficient strategy uses a greedy counter approach. Traverse the string and track unmatched opening brackets. When encountering a closing bracket, try to match it with a previous opening bracket. If no match exists, it means an opening bracket must be added. At the end of the traversal, any remaining unmatched opening brackets require corresponding closing brackets.

An alternative way to reason about the problem is using a stack to simulate matching parentheses, pushing openings and popping when a valid closing appears. However, a simple counter-based greedy method achieves the same result with less overhead.

The optimal solution runs in O(n) time by scanning the string once and uses O(1) space when implemented with counters.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy CounterO(n)O(1)
Stack-Based MatchingO(n)O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (12)

Greedy Balance Tracking

This approach involves iterating through the string and using a balance counter to track the validity of the sequence.

We increment the balance for every opening parenthesis '(' and decrement it for a closing parenthesis ')'. If, at any point, the balance becomes negative, it means we have an extra closing parenthesis, and we need to increase the count of steps required to balance the string. By the end of the iteration, the balance will also tell us how many opening parentheses need to be closed to make the string valid.

Time Complexity: O(n) - We traverse the string once.
Space Complexity: O(1) - Only a few integer variables are used.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4int minAddToMakeValid(char * s) {
5    int balance = 0,

Explanation

The C implementation uses two variables, balance and result. We iterate over the string and adjust the balance for each parenthesis. Whenever balance is negative, it indicates an unmatched ')', and we increase the result to account for the needed '('. Finally, we return the sum of result and balance, where balance accounts for unmatched '('.

Stack Simulation

In this approach, we leverage a stack structure to simulate parentheses placement, assisting in counting the balance.

Iterate over the string, pushing opening parentheses onto the stack and popping for each encountered closing parenthesis when possible. When a closing parenthesis finds no partner to pop, increase the invalid count, simulating the need for one more opening parenthesis. In the end, the stack length represents unmatched opening parentheses.

Time Complexity: O(n) - Iterate over the string once.
Space Complexity: O(1) - Uses a few integer counters.

CC++JavaPythonC#JavaScript


Video Solutions

Watch expert explanations and walkthroughs

Minimum Remove to Make Valid Parentheses - Leetcode 1249 - Python

NeetCodeIO
14:4324,467 views

Asked By Companies

4 companies
F
Facebook
M
Microsoft
A
Amazon
T
Twitter

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

StringStackGreedy

Problem Stats

Acceptance Rate74.7%
DifficultyMedium
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does the greedy approach work for this problem?

The greedy approach works because each parenthesis can be locally matched or identified as unmatched during a single pass. By immediately resolving matches or counting missing counterparts, you ensure the minimal number of additions required to make the string valid.

Is Minimum Add to Make Parentheses Valid asked in coding interviews?

Yes, variations of this problem frequently appear in coding interviews. Companies often test candidates on parentheses validation, stack usage, and greedy reasoning. It helps evaluate a candidate's ability to reason about string processing and edge cases.

What data structure is best for Minimum Add to Make Parentheses Valid?

While a stack can be used to simulate matching parentheses, it is not strictly necessary. A simple counter-based greedy approach is more space-efficient and achieves the same result. However, understanding the stack method helps build intuition for parentheses matching problems.

What is the optimal approach for Minimum Add to Make Parentheses Valid?

The optimal approach uses a greedy counter technique. By scanning the string once and tracking unmatched opening parentheses, you can determine how many additions are needed. This approach runs in O(n) time and uses O(1) extra space.

result
=
0
;
6
for
(
int
i
=
0
;
i
<
strlen
(
s
)
;
i
++
)
{
7
balance
+=
(
s
[
i
]
==
'('
)
?
1
:
-
1
;
8
if
(
balance
==
-
1
)
{
9
result
++
;
10
balance
++
;
11
}
12
}
13
return
result
+
balance
;
14
}
15
16
int
main
(
)
{
17
char
s
[
]
=
")(("
;
18
printf
(
"%d\n"
,
minAddToMakeValid
(
s
)
)
;
19
return
0
;
20
}
1
#
include
<stdio.h>
2
#
include
<string.h>
3
4
int
minAddToMakeValid
(
char
*
s
)
{
5
int
stack
=
0
,
invalid
=
0
;
6
for
(
int
i
=
0
;
i
<
strlen
(
s
)
;
i
++
)
{
7
if
(
s
[
i
]
==
'('
)
{
8
stack
++
;
9
}
else
{
10
if
(
stack
>
0
)
{
11
stack
--
;
12
}
else
{
13
invalid
++
;
14
}
15
}
16
}
17
return
invalid
+
stack
;
18
}
19
20
int
main
(
)
{
21
char
s
[
]
=
")(()"
;
22
printf
(
"%d\n"
,
minAddToMakeValid
(
s
)
)
;
23
return
0
;
24
}

Explanation

Here, we maintain a stack variable to track open parentheses and an invalid count for unmatched closing parentheses. This eliminates the need for an actual stack structure, only integers are enough.