20250317 finished.

This commit is contained in:
jackfiled 2025-03-17 12:02:00 +08:00
parent 29372e18ab
commit 8285dd6654
2 changed files with 42 additions and 0 deletions

View File

@ -542,3 +542,5 @@ mod p3340_check_balanced_string;
mod p3110_score_of_a_string;
mod p2272_substring_with_largest_variance;
mod p1963_minimum_number_of_swaps_to_make_the_string_balanced;

View File

@ -0,0 +1,40 @@
/**
* [1963] Minimum Number of Swaps to Make the String Balanced
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_swaps(s: String) -> i32 {
let mut count = 0;
let mut min_count = 0;
for c in s.chars() {
match c {
'[' => count += 1,
']' => {
count -= 1;
min_count = min_count.min(count)
}
_ => {}
}
}
(-min_count + 1) / 2
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1963() {
assert_eq!(1, Solution::min_swaps("][][".to_owned()));
assert_eq!(2, Solution::min_swaps("]]][[[".to_owned()));
assert_eq!(0, Solution::min_swaps("[]".to_owned()));
}
}