Skip to content

Commit 5686421

Browse files
committed
LC 921. Minimum Add to Make Parentheses Valid (Rust)
1 parent 1332403 commit 5686421

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ to the solution in this repository.
416416
| [912. Sort an Array][lc912] | 🟠 Medium | [![python](res/py.png)][lc912py] [![rust](res/rs.png)][lc912rs] |
417417
| [916. Word Subsets][lc916] | 🟠 Medium | [![python](res/py.png)][lc916py] |
418418
| [918. Maximum Sum Circular Subarray][lc918] | 🟠 Medium | [![python](res/py.png)][lc918py] [![rust](res/rs.png)][lc918rs] |
419+
| [921. Minimum Add to Make Parentheses Valid][lc921] | 🟠 Medium | [![rust](res/rs.png)][lc921rs] |
419420
| [926. Flip String to Monotone Increasing][lc926] | 🟠 Medium | [![python](res/py.png)][lc926py] [![rust](res/rs.png)][lc926rs] |
420421
| [930. Binary Subarrays With Sum][lc930] | 🟠 Medium | [![rust](res/rs.png)][lc930rs] |
421422
| [931. Minimum Falling Path Sum][lc931] | 🟠 Medium | [![python](res/py.png)][lc931py] |
@@ -1674,6 +1675,8 @@ to the solution in this repository.
16741675
[lc918]: https://leetcode.com/problems/maximum-sum-circular-subarray/
16751676
[lc918py]: leetcode/maximum-sum-circular-subarray.py
16761677
[lc918rs]: leetcode/maximum-sum-circular-subarray.rs
1678+
[lc921]: https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
1679+
[lc921rs]: leetcode/minimum-add-to-make-parentheses-valid.rs
16771680
[lc926]: https://leetcode.com/problems/flip-string-to-monotone-increasing/
16781681
[lc926py]: leetcode/flip-string-to-monotone-increasing.py
16791682
[lc926rs]: leetcode/flip-string-to-monotone-increasing.rs
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 921. Minimum Add to Make Parentheses Valid
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
5+
//
6+
// Tags: String - Stack - Greedy
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate over the input keeping track of the number of open left parentheses we find, for
11+
/// each unmatched or extra parentheses we will need to add a matching one.
12+
///
13+
/// Time complexity: O(n)
14+
/// Space complexity: O(1)
15+
///
16+
/// Runtime 1 ms Beats 58%
17+
/// Memory 2.18 MB Beats 23%
18+
pub fn min_add_to_make_valid(s: String) -> i32 {
19+
let (mut left, mut right) = (0, 0);
20+
for c in s.chars() {
21+
if c == '(' {
22+
right += 1;
23+
} else if right > 0 {
24+
right -= 1;
25+
} else {
26+
left += 1;
27+
}
28+
}
29+
right + left
30+
}
31+
}
32+
33+
// Tests.
34+
fn main() {
35+
let tests = [("())", 1), ("(((", 3)];
36+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
37+
let mut success = 0;
38+
for (i, t) in tests.iter().enumerate() {
39+
let res = Solution::min_add_to_make_valid(t.0.to_string());
40+
if res == t.1 {
41+
success += 1;
42+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
43+
} else {
44+
println!(
45+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
46+
i, t.1, res
47+
);
48+
}
49+
}
50+
println!();
51+
if success == tests.len() {
52+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
53+
} else if success == 0 {
54+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
55+
} else {
56+
println!(
57+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
58+
tests.len() - success
59+
)
60+
}
61+
}

0 commit comments

Comments
 (0)