Skip to content

Commit ec2298a

Browse files
committed
New Solution "1541. Minimum Insertions to Balance a Parentheses String"
1 parent bad3dcd commit ec2298a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LeetCode
1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
1212
|1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium|
13+
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium|
1314
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium|
1415
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/) | [C++](./algorithms/cpp/shuffleTheArray/ShuffleTheArray.cpp)|Easy|
1516
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [C++](./algorithms/cpp/maximumProductOfTwoElementsInAnArray/MaximumProductOfTwoElementsInAnArray.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Source : https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/
2+
// Author : Hao Chen
3+
// Date : 2020-10-02
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is
8+
* balanced if:
9+
*
10+
* Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
11+
* Left parenthesis '(' must go before the corresponding two consecutive right parenthesis
12+
* '))'.
13+
*
14+
* In other words, we treat '(' as openning parenthesis and '))' as closing parenthesis.
15+
*
16+
* For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not
17+
* balanced.
18+
*
19+
* You can insert the characters '(' and ')' at any position of the string to balance it if needed.
20+
*
21+
* Return the minimum number of insertions needed to make s balanced.
22+
*
23+
* Example 1:
24+
*
25+
* Input: s = "(()))"
26+
* Output: 1
27+
* Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need
28+
* to to add one more ')' at the end of the string to be "(())))" which is balanced.
29+
*
30+
* Example 2:
31+
*
32+
* Input: s = "())"
33+
* Output: 0
34+
* Explanation: The string is already balanced.
35+
*
36+
* Example 3:
37+
*
38+
* Input: s = "))())("
39+
* Output: 3
40+
* Explanation: Add '(' to match the first '))', Add '))' to match the last '('.
41+
*
42+
* Example 4:
43+
*
44+
* Input: s = "(((((("
45+
* Output: 12
46+
* Explanation: Add 12 ')' to balance the string.
47+
*
48+
* Example 5:
49+
*
50+
* Input: s = ")))))))"
51+
* Output: 5
52+
* Explanation: Add 4 '(' at the beginning of the string and one ')' at the end. The string becomes
53+
* "(((())))))))".
54+
*
55+
* Constraints:
56+
*
57+
* 1 <= s.length <= 10^5
58+
* s consists of '(' and ')' only.
59+
******************************************************************************************************/
60+
class Solution {
61+
public:
62+
int minInsertions(string s) {
63+
vector<char> stack;
64+
65+
int cnt = 0;
66+
int len = s.size();
67+
for (int i=0; i<len; i++) {
68+
69+
if ( s[i] == '(' ) {
70+
stack.push_back( s[i] );
71+
continue;
72+
}
73+
// if s[i] is ')'
74+
if (stack.size() > 0) {
75+
stack.pop_back();
76+
} else {
77+
cnt++; // missed the '('
78+
}
79+
80+
// if s[i+1] is ')', need to skip
81+
if ( i < len -1 && s[i+1] == ')' ) {
82+
i++;
83+
}else{
84+
cnt++; //missed the ')'
85+
}
86+
87+
}
88+
89+
// if the stack still has '(', which means need double of ')'
90+
return cnt + stack.size()*2;
91+
}
92+
};

0 commit comments

Comments
 (0)