[LeetCode] 921. Minimum Add to Make Parentheses Valid 使括號有效的最少添加



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

  • It is the empty string, or
  • 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.

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

  1. S.length <= 1000
  2. 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/



LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索