-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0301_remove_invalid_parentheses.rs
More file actions
74 lines (70 loc) · 2.15 KB
/
s0301_remove_invalid_parentheses.rs
File metadata and controls
74 lines (70 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(2^n) O(N)
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
use std::iter::FromIterator;
fn remove(s: &[char], ans: &mut Vec<String>, last_i: i32, last_j: i32, par: &[char]) {
let mut stack = 0;
for i in last_i..s.len() as i32 {
if s[i as usize] == par[0] {
stack += 1;
}
if s[i as usize] == par[1] {
stack -= 1;
}
if stack >= 0 {
continue;
}
for j in last_j..=i {
if s[j as usize] == par[1] && (j == last_j || s[j as usize - 1] != par[1]) {
let mut s2 = Vec::<char>::new();
for k in 0..j as usize {
s2.push(s[k]);
}
for k in j as usize + 1..s.len() {
s2.push(s[k]);
}
remove(&s2, ans, i, j, par);
}
}
return;
}
let mut reversed = s.to_vec();
reversed.reverse();
if par[0] == '(' {
remove(&reversed, ans, 0, 0, &[')', '(']);
} else {
ans.push(String::from_iter(reversed));
}
}
let mut ans = vec![];
remove(
&s.chars().collect::<Vec<char>>(),
&mut ans,
0,
0,
&['(', ')'],
);
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_301() {
assert_eq!(
Solution::remove_invalid_parentheses("()())()".to_owned()),
vec!["(())()".to_owned(), "()()()".to_owned(),]
);
assert_eq!(
Solution::remove_invalid_parentheses("(a)())()".to_owned()),
vec!["(a())()".to_owned(), "(a)()()".to_owned(),]
);
assert_eq!(
Solution::remove_invalid_parentheses(")(".to_owned()),
vec!["".to_owned()]
);
}
}