
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum insertions to balance a parentheses string using Python
Suppose we have a string s with opening and closing parenthesis '(' and ')'. We can say a parentheses string is balanced when −
Any left parenthesis '(' have a corresponding two consecutive right parenthesis '))'.
A Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.
So for example, "())", "())(())))" are balanced but ")()", "()))" are not. If we have such string, we have to count number of parenthesis (opening or closing) to make string balanced.
So, if the input is like s = "(())))))", then the output will be 1 because if we split it up, we can get "( ()) )) ))", so we need one left parenthesis to make the string "( ()) ()) ))" to make it balanced.
To solve this, we will follow these steps −
:= 0, n := size of s
ret := 0, i := 0
while i < n, do
if s[i] is same as '(', then
:= o + 1
otherwise,
if i + 1 < n and s[i + 1] is same as ')', then
if o is 0, then
ret := ret + 1
otherwise,
:= o - 1
i := i + 1
otherwise,
ret := ret + 1
if o is 0, then
ret := ret + 1
otherwise,
:= o - 1
i := i + 1
return ret + 2 * o
Let us see the following implementation to get better understanding −
Example
def solve(s): o = 0 n = len(s) ret = 0 i = 0 while i < n: if s[i] == '(': o += 1 else: if i + 1 < n and s[i + 1] == ')': if not o: ret += 1 else: o -= 1 i += 1 else: ret += 1 if not o: ret += 1 else: o -= 1 i += 1 return ret + 2 * o s = "(())))))" print(solve(s))
Input
"(())))))"
Output
3
- Related Articles
- Program to find minimum remove required to make valid parentheses in Python
- Minimum number of deletions and insertions to transform one string into another using C++.
- Minimum Add to Make Parentheses Valid in Python
- Program to find minimum deletions to make string balanced in Python
- Cost to Balance the parentheses in C++
- Minimum insertions to make a Co-prime array in C++
- Program to find minimum swaps to arrange a binary grid using Python
- Program to find minimum number of monotonous string groups in Python
- Program to find minimum distance to the target element using Python
- Program to find minimum operations to make array equal using Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find minimum string size that contains given substring in Python
- Program to find minimum changes required for alternating binary string in Python
- Program to find minimum operations to make the array increasing using Python
- JavaScript Program to Find Lexicographically minimum string rotation
