Compare commits
4
Commits
a2f7ac206a
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fcb9344f99 | ||
|
|
582e1f1375 | ||
|
|
9f268359d2 | ||
|
|
854d77f03f |
+38
-47
@@ -1,4 +1,3 @@
|
|||||||
use crate::arr::make_char_matrix;
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
@@ -6,61 +5,53 @@ mod list;
|
|||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn has_valid_path(grid: Vec<Vec<char>>) -> bool {
|
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
|
||||||
if grid[0][0] == ')'
|
let s = s.as_bytes().to_vec();
|
||||||
|| grid[grid.len() - 1][grid[0].len() - 1] == '('
|
let mut queue1 = HashSet::new();
|
||||||
|| (grid.len() + grid[0].len()) % 2 == 0
|
fn valid(x: &Vec<u8>) -> bool {
|
||||||
{
|
let mut cnt = 0;
|
||||||
return false;
|
for c in x {
|
||||||
}
|
if *c == b'(' {
|
||||||
let mut matrix = vec![vec![Option::<HashSet<i32>>::None; grid[0].len()]; grid.len()];
|
cnt += 1;
|
||||||
matrix[0][0] = Some(HashSet::from_iter([10000]));
|
} else if *c == b')' {
|
||||||
for i in 0..=(grid.len() - 1 + grid[0].len() - 1) {
|
cnt -= 1;
|
||||||
for j in 0..=i {
|
if cnt < 0 {
|
||||||
if grid.len() > j && grid[0].len() > i - j {
|
return false;
|
||||||
if grid.len() > j + 1 {
|
|
||||||
let (rsrc, rdst) = matrix.split_at_mut(j + 1);
|
|
||||||
let dst = rdst[0][i - j].get_or_insert_with(HashSet::new);
|
|
||||||
let src = rsrc[j][i - j].as_ref().unwrap();
|
|
||||||
if grid[j + 1][i - j] == '(' {
|
|
||||||
dst.extend(src.iter().map(|&x| x + 10000));
|
|
||||||
} else {
|
|
||||||
dst.extend(
|
|
||||||
src.iter()
|
|
||||||
.filter(|&x| x / 10000 > x % 10000)
|
|
||||||
.map(|&x| x + 1),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if grid[0].len() > i - j + 1 {
|
|
||||||
let (rsrc, rdst) = matrix[j].split_at_mut(i - j + 1);
|
|
||||||
let dst = rdst[0].get_or_insert_with(HashSet::new);
|
|
||||||
let src = rsrc[i - j].as_ref().unwrap();
|
|
||||||
if grid[j][i - j + 1] == '(' {
|
|
||||||
dst.extend(src.iter().map(|&x| x + 10000));
|
|
||||||
} else {
|
|
||||||
dst.extend(
|
|
||||||
src.iter()
|
|
||||||
.filter(|&x| x / 10000 > x % 10000)
|
|
||||||
.map(|&x| x + 1),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cnt == 0
|
||||||
|
}
|
||||||
|
queue1.insert(s);
|
||||||
|
loop {
|
||||||
|
{
|
||||||
|
let mut local_ans = vec![];
|
||||||
|
for x in queue1.iter() {
|
||||||
|
if valid(&x) {
|
||||||
|
local_ans.push(x.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !local_ans.is_empty() {
|
||||||
|
return local_ans
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| x.iter().map(|&y| y as char).collect::<String>())
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut queue2 = HashSet::<Vec<u8>>::new();
|
||||||
|
for x in queue1.into_iter(){
|
||||||
|
for i in 0..x.len() {
|
||||||
|
queue2.insert( x[..i].iter().chain(x[i + 1..].iter()).copied().collect());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue1 = queue2;
|
||||||
}
|
}
|
||||||
matrix[grid.len() - 1][grid[0].len() - 1].as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.iter()
|
|
||||||
.any(|&x| x / 10000 == x % 10000)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
Solution::has_valid_path(make_char_matrix(
|
Solution::remove_invalid_parentheses(")()())()))(((()()()(()".to_string())
|
||||||
r#"[["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]"#
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user