[LeetCode] 1541. Minimum Insertions to Balance a Parentheses String

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:

Input: s = "(()))"
Output: 1
Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to to add one more ')' at the end of the string to be "(())))" which is balanced.

Example 2:

Input: s = "())"
Output: 0
Explanation: The string is already balanced.

Example 3:

Input: s = "))())("
Output: 3
Explanation: Add '(' to match the first '))', Add '))' to match the last '('.

Example 4:

Input: s = "(((((("
Output: 12
Explanation: Add 12 ')' to balance the string.

Example 5:

Input: s = ")))))))"
Output: 5
Explanation: Add 4 '(' at the beginning of the string and one ')' at the end. The string becomes "(((())))))))".

Constraints:

  • 1 <= s.length <= 10^5
  • s consists of '(' and ')' only.

平衡括号字符串的最少插入次数。

给你一个括号字符串 s ,它只包含字符 '(' 和 ')' 。一个括号字符串被称为平衡的当它满足:

任何左括号 '(' 必须对应两个连续的右括号 '))' 。
左括号 '(' 必须在对应的连续两个右括号 '))' 之前。
比方说 "())", "())(())))" 和 "(())())))" 都是平衡的, ")()", "()))" 和 "(()))" 都是不平衡的。

你可以在任意位置插入字符 '(' 和 ')' 使字符串平衡。

请你返回让 s 平衡的最少插入次数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-insertions-to-balance-a-parentheses-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意不难理解,就是按照规则进行配对,返回需要增加的括号数量。注意这里只能增加不能删去,而且注意配对的规则是一个左括号配对两个右括号。

思路类似大部分括号配对的题目但是不需要用到栈。这里我们需要两个变量,left记录左括号出现的次数,res记录需要增加的括号的数量。遍历input字符串,如果遇到左括号就left++,遇到右括号的话,分两种情况

  • 当前位置的下一个位置依然是右括号,说明有两个连续的右括号。此时判断是否已经有左括号等待被抵消,如果没有则res++
  • 只出现了一个单独的右括号,此时判断是否有左括号等待被抵消,若有则只需要增加一个右括号,若没有则需要左右各增加一个,即res += 2

注意最后返回res的时候还需要再次检查是否有没有被抵消的left存在,若有,则还需要增加 2 * left 个右括号才行。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public int minInsertions(String s) {
 3         int left = 0;
 4         int res = 0;
 5         for (int i = 0; i < s.length(); i++) {
 6             char c = s.charAt(i);
 7             if (c == '(') {
 8                 left++;
 9             } else if (c == ')') {
10                 if (i + 1 < s.length() && s.charAt(i + 1) == ')') {
11                     if (left > 0) {
12                         left--;
13                     } else {
14                         res++;
15                     }
16                     i++;
17                 } else {
18                     if (left > 0) {
19                         left--;
20                         res++;
21                     } else {
22                         res += 2;
23                     }
24                 }
25             }
26         }
27         res += left * 2;
28         return res;
29     }
30 }

相关题目

20. Valid Parentheses

678. Valid Parenthesis String

1111. Maximum Nesting Depth of Two Valid Parentheses Strings

921. Minimum Add to Make Parentheses Valid

1541. Minimum Insertions to Balance a Parentheses String

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/14153089.html