Given a string S
of '('
and ')'
parentheses, we add the minimum number of parentheses ( '('
or ')'
, and in any positions ) so that the resulting parentheses string is valid.html
Formally, a parentheses string is valid if and only if:git
AB
(A
concatenated with B
), where A
and B
are valid strings, or(A)
, where A
is a valid string.Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.github
Example 1:code
Input: "())" Output: 1
Example 2:orm
Input: "(((" Output: 3
Example 3:htm
Input: "()" Output: 0
Example 4:blog
Input: "()))((" Output: 4
Note:leetcode
S.length <= 1000
S
only consists of '('
and ')'
characters.
Github 同步地址:get
https://github.com/grandyang/leetcode/issues/CHANGE_ME同步
相似題目:
參考資料:
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/