Skip to content

Commit ce72ca6

Browse files
committed
1541. Minimum Insertions to Balance a Parentheses String: AC
1 parent cce3731 commit ce72ca6

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1162,3 +1162,4 @@ mod s1536_minimum_swaps_to_arrange_a_binary_grid;
11621162
mod s1537_get_the_maximum_score;
11631163
mod s1539_kth_missing_positive_number;
11641164
mod s1540_can_convert_string_in_k_moves;
1165+
mod s1541_minimum_insertions_to_balance_a_parentheses_string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* [1541] Minimum Insertions to Balance a Parentheses String
3+
*
4+
* Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is balanced if:
5+
*
6+
* Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
7+
* Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.
8+
*
9+
* In other words, we treat '(' as an opening parenthesis and '))' as a closing parenthesis.
10+
*
11+
* For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not balanced.
12+
*
13+
* You can insert the characters '(' and ')' at any position of the string to balance it if needed.
14+
* Return the minimum number of insertions needed to make s balanced.
15+
*
16+
* Example 1:
17+
*
18+
* Input: s = "(()))"
19+
* Output: 1
20+
* Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to add one more ')' at the end of the string to be "(())))" which is balanced.
21+
*
22+
* Example 2:
23+
*
24+
* Input: s = "())"
25+
* Output: 0
26+
* Explanation: The string is already balanced.
27+
*
28+
* Example 3:
29+
*
30+
* Input: s = "))())("
31+
* Output: 3
32+
* Explanation: Add '(' to match the first '))', Add '))' to match the last '('.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= s.length <= 10^5
38+
* s consists of '(' and ')' only.
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/
44+
// discuss: https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn min_insertions(s: String) -> i32 {
50+
let (mut result, mut right) = (0, 0);
51+
let s = s.as_bytes();
52+
for &c in s.iter() {
53+
if c == b'(' {
54+
if right % 2 > 0 {
55+
right -= 1;
56+
result += 1;
57+
}
58+
right += 2;
59+
} else {
60+
right -= 1;
61+
if right < 0 {
62+
right += 2;
63+
result += 1;
64+
}
65+
}
66+
}
67+
right + result
68+
}
69+
}
70+
71+
// submission codes end
72+
73+
#[cfg(test)]
74+
mod tests {
75+
use super::*;
76+
77+
#[test]
78+
fn test_1541_example_1() {
79+
let s = "(()))".to_string();
80+
81+
let result = 1;
82+
83+
assert_eq!(Solution::min_insertions(s), result);
84+
}
85+
86+
#[test]
87+
fn test_1541_example_2() {
88+
let s = "())".to_string();
89+
90+
let result = 0;
91+
92+
assert_eq!(Solution::min_insertions(s), result);
93+
}
94+
95+
#[test]
96+
fn test_1541_example_3() {
97+
let s = "))())(".to_string();
98+
99+
let result = 3;
100+
101+
assert_eq!(Solution::min_insertions(s), result);
102+
}
103+
}

0 commit comments

Comments
 (0)