20250323 finished.
This commit is contained in:
parent
3846eebd59
commit
9ec00796bb
|
@ -554,3 +554,5 @@ mod p2612_minimum_reverse_operations;
|
||||||
mod p2680_maximum_or;
|
mod p2680_maximum_or;
|
||||||
|
|
||||||
mod p2643_row_with_maximum_ones;
|
mod p2643_row_with_maximum_ones;
|
||||||
|
|
||||||
|
mod p2116_check_if_a_parentheses_string_can_be_valid;
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
/**
|
||||||
|
* [2116] Check if a Parentheses String Can Be Valid
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn can_be_valid(s: String, locked: String) -> bool {
|
||||||
|
let s: Vec<u8> = s.bytes().collect();
|
||||||
|
let locked: Vec<bool> = locked
|
||||||
|
.bytes()
|
||||||
|
.map(|x| if x == b'0' { false } else { true })
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if s.len() % 2 != 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut locked_left = vec![];
|
||||||
|
let mut unlocked_pos = vec![];
|
||||||
|
|
||||||
|
for (i, &v) in s.iter().enumerate() {
|
||||||
|
if !locked[i] {
|
||||||
|
unlocked_pos.push(i);
|
||||||
|
} else {
|
||||||
|
if v == b'(' {
|
||||||
|
locked_left.push(i);
|
||||||
|
} else {
|
||||||
|
// 首先使用锁定的左括号匹配
|
||||||
|
if locked_left.pop().is_none() {
|
||||||
|
// 然后使用未锁定的位置修改
|
||||||
|
if unlocked_pos.pop().is_none() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断剩余的未锁定位置和锁定的左括号是否匹配
|
||||||
|
while let Some(left_pos) = locked_left.pop() {
|
||||||
|
if let Some(unlock_pos) = unlocked_pos.pop() {
|
||||||
|
if left_pos > unlock_pos {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 剩余的未锁定括号必须是偶数个
|
||||||
|
unlocked_pos.len() % 2 == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2116() {
|
||||||
|
assert!(!Solution::can_be_valid(
|
||||||
|
"(((())".to_owned(),
|
||||||
|
"111111".to_owned()
|
||||||
|
));
|
||||||
|
assert!(Solution::can_be_valid(
|
||||||
|
"))()))".to_owned(),
|
||||||
|
"010100".to_owned()
|
||||||
|
));
|
||||||
|
assert!(Solution::can_be_valid("()()".to_owned(), "0000".to_owned()));
|
||||||
|
assert!(!Solution::can_be_valid(")".to_owned(), "0".to_owned()));
|
||||||
|
assert!(Solution::can_be_valid(
|
||||||
|
"(((())(((())".to_owned(),
|
||||||
|
"111111010111".to_owned()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user