- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum remove required to make valid parentheses in Python

Suppose we have a string s with parenthesis '(' , ')' and lowercase English characters. We have to delete the minimum number of parentheses ( '(' or ')', from any positions ) so that the resulting parentheses string is valid and have to finally return any valid string. Here the parentheses string is valid when all of these criteria are fulfilled −

The string is empty and contains lowercase characters only, or

The string can be written as AB (A concatenated with B), where A and B are valid strings, or

The string can be written as the form of (A), where A is a valid string.

So, if the input is like s = "m)n(o)p", then the output will be "mn(o)p"

To solve this, we will follow these steps −

stack := a new list

indexes := a new set

i := 0

for each c in s, do

if c is same as '(', then

push i into stack

otherwise when c is same as ')', then

if size of stack is same as 0, then

insert i into indexes

otherwise,

pop from stack

i := i + 1

ret := blank string

indexes := store all elements into indexes

for i in range 0 to size of s - 1, do

if i not in indexes, then

ret := ret + s[i]

return ret

## Example

Let us see the following implementation to get better understanding −

def solve(s): stack = [] indexes = set() i = 0 for c in s: if c == '(': stack.append(i) elif c == ')': if len(stack) == 0: indexes.add(i) else: stack.pop() i += 1 ret = '' indexes = indexes.union(stack) for i in range(len(s)): if i not in indexes: ret += s[i] return ret s = "m)n(o)p" print(solve(s))

## Input

"m)n(o)p"

## Output

mn(o)p

- Related Questions & Answers
- Minimum Remove to Make Valid Parentheses in C++
- Minimum Add to Make Parentheses Valid in Python
- Program to find minimum swaps required to make given anagram in python
- Minimum number of Parentheses to be added to make it valid in C++
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Longest Valid Parentheses in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum insertions to balance a parentheses string using Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Minimum edges required to add to make Euler Circuit in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Valid Parentheses in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?