Skip to content

Commit 3dd9a8f

Browse files
committed
LC 1963. Minimum Number of Swaps to Make the String Balanced (Rust)
1 parent 4f3b4d1 commit 3dd9a8f

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ to the solution in this repository.
636636
| [1942. The Number of the Smallest Unoccupied Chair][lc1942] | 🟠 Medium | [![rust](res/rs.png)][lc1942rs] |
637637
| [1945. Sum of Digits of String After Convert][lc1945] | 🟢 Easy | [![rust](res/rs.png)][lc1945rs] |
638638
| [1962. Remove Stones to Minimize the Total][lc1962] | 🟠 Medium | [![python](res/py.png)][lc1962py] |
639+
| [1963. Minimum Number of Swaps to Make the String Balanced][lc1963] | 🟠 Medium | [![rust](res/rs.png)][lc1963rs] |
639640
| [1964. Find the Longest Valid Obstacle Course at Each Position][lc1964] | 🔴 Hard | [![rust](res/rs.png)][lc1964rs] |
640641
| [1970. Last Day Where You Can Still Cross][lc1970] | 🔴 Hard | [![rust](res/rs.png)][lc1970rs] |
641642
| [1971. Find if Path Exists in Graph][lc1971] | 🟢 Easy | [![python](res/py.png)][lc1971py] |
@@ -2172,6 +2173,8 @@ to the solution in this repository.
21722173
[lc1945rs]: leetcode/sum-of-digits-of-string-after-convert.rs
21732174
[lc1962]: https://leetcode.com/problems/remove-stones-to-minimize-the-total/
21742175
[lc1962py]: leetcode/remove-stones-to-minimize-the-total.py
2176+
[lc1963]: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/
2177+
[lc1963rs]: leetcode/minimum-number-of-swaps-to-make-the-string-balanced.rs
21752178
[lc1964]: https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/
21762179
[lc1964rs]: leetcode/find-the-longest-valid-obstacle-course-at-each-position.rs
21772180
[lc1970]: https://leetcode.com/problems/last-day-where-you-can-still-cross/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// 1963. Minimum Number of Swaps to Make the String Balanced
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/
5+
//
6+
// Tags: Two Pointers - String - Stack - Greedy
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate the string, when the number of closing brackets is greater
11+
/// than the number of opening brackets, we swap one with the furthest right opening bracket in
12+
/// the string. We can do that using and updating counts rather than actually updating the
13+
/// input string.
14+
///
15+
/// Time complexity: O(n) - We visit each character in the input and do constant time work for
16+
/// each.
17+
/// Space complexity: O(1)
18+
///
19+
/// Runtime 8 ms Beats 76%
20+
/// Memory 4.70 MB Beats 75%
21+
pub fn min_swaps(s: String) -> i32 {
22+
// Not needed but helps with early break.
23+
let mut remaining_opening = s.len() / 2;
24+
let mut swaps = 0;
25+
let (mut opening, mut closing) = (0, 0);
26+
for c in s.chars() {
27+
match c {
28+
'[' => {
29+
opening += 1;
30+
remaining_opening -= 1;
31+
}
32+
_ => closing += 1,
33+
}
34+
if closing > opening {
35+
swaps += 1;
36+
remaining_opening -= 1;
37+
opening += 1;
38+
closing -= 1;
39+
}
40+
if remaining_opening == 0 {
41+
break;
42+
}
43+
}
44+
swaps
45+
}
46+
}
47+
48+
// Tests.
49+
fn main() {
50+
let tests = [("][][", 1), ("]]][[[", 2), ("[]", 0)];
51+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
52+
let mut success = 0;
53+
for (i, t) in tests.iter().enumerate() {
54+
let res = Solution::min_swaps(t.0.to_string());
55+
if res == t.1 {
56+
success += 1;
57+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
58+
} else {
59+
println!(
60+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
61+
i, t.1, res
62+
);
63+
}
64+
}
65+
println!();
66+
if success == tests.len() {
67+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
68+
} else if success == 0 {
69+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
70+
} else {
71+
println!(
72+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
73+
tests.len() - success
74+
)
75+
}
76+
}

0 commit comments

Comments
 (0)