Skip to content

Commit 599f5e7

Browse files
committed
1963. Minimum Number of Swaps to Make the String Balanced: AC
1 parent 7b33346 commit 599f5e7

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Diff for: src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1480,3 +1480,4 @@ mod s1959_minimum_total_space_wasted_with_k_resizing_operations;
14801480
mod s1960_maximum_product_of_the_length_of_two_palindromic_substrings;
14811481
mod s1961_check_if_string_is_a_prefix_of_array;
14821482
mod s1962_remove_stones_to_minimize_the_total;
1483+
mod s1963_minimum_number_of_swaps_to_make_the_string_balanced;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* [1963] Minimum Number of Swaps to Make the String Balanced
3+
*
4+
* You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.
5+
* A string is called balanced if and only if:
6+
*
7+
* It is the empty string, or
8+
* It can be written as AB, where both A and B are balanced strings, or
9+
* It can be written as [C], where C is a balanced string.
10+
*
11+
* You may swap the brackets at any two indices any number of times.
12+
* Return the minimum number of swaps to make s balanced.
13+
*
14+
* Example 1:
15+
*
16+
* Input: s = "][]["
17+
* Output: 1
18+
* Explanation: You can make the string balanced by swapping index 0 with index 3.
19+
* The resulting string is "[[]]".
20+
*
21+
* Example 2:
22+
*
23+
* Input: s = "]]][[["
24+
* Output: 2
25+
* Explanation: You can do the following to make the string balanced:
26+
* - Swap index 0 with index 4. s = "[]][][".
27+
* - Swap index 1 with index 5. s = "[[][]]".
28+
* The resulting string is "[[][]]".
29+
*
30+
* Example 3:
31+
*
32+
* Input: s = "[]"
33+
* Output: 0
34+
* Explanation: The string is already balanced.
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* n == s.length
40+
* 2 <= n <= 10^6
41+
* n is even.
42+
* s[i] is either '[' or ']'.
43+
* The number of opening brackets '[' equals n / 2, and the number of closing brackets ']' equals n / 2.
44+
*
45+
*/
46+
pub struct Solution {}
47+
48+
// problem: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/
49+
// discuss: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/discuss/?currentPage=1&orderBy=most_votes&query=
50+
51+
// submission codes start here
52+
53+
impl Solution {
54+
pub fn min_swaps(s: String) -> i32 {
55+
s.chars().fold(1, |acc, c| {
56+
if c == '[' {
57+
acc + 1
58+
} else if c == ']' && acc > 1 {
59+
acc - 1
60+
} else {
61+
acc
62+
}
63+
}) / 2
64+
}
65+
}
66+
67+
// submission codes end
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test_1963_example_1() {
75+
let s = "][][".to_string();
76+
77+
let result = 1;
78+
79+
assert_eq!(Solution::min_swaps(s), result);
80+
}
81+
82+
#[test]
83+
fn test_1963_example_2() {
84+
let s = "]]][[[".to_string();
85+
86+
let result = 2;
87+
88+
assert_eq!(Solution::min_swaps(s), result);
89+
}
90+
91+
#[test]
92+
fn test_1963_example_3() {
93+
let s = "[]".to_string();
94+
95+
let result = 0;
96+
97+
assert_eq!(Solution::min_swaps(s), result);
98+
}
99+
}

0 commit comments

Comments
 (0)