1541. Minimum Insertions to Balance a Parentheses String
https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string
Description
Given a parentheses string s
containing only the characters '('
and ')'
. A parentheses string is balanced if:
Any left parenthesis
'('
must have a corresponding two consecutive right parenthesis'))'
.Left parenthesis
'('
must go before the corresponding two consecutive right parenthesis'))'
.
In other words, we treat '('
as openning parenthesis and '))'
as closing parenthesis.
For example, "())"
, "())(())))"
and "(())())))"
are balanced, ")()"
, "()))"
and "(()))"
are not balanced.
You can insert the characters '('
and ')'
at any position of the string to balance it if needed.
Return the minimum number of insertions needed to make s
balanced.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
1 <= s.length <= 10^5
s
consists of'('
and')'
only.
ac
Last updated