Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d20981d

Browse files
committed
Solve #301
1 parent 5113a9f commit d20981d

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

‎src/n0301_remove_invalid_parentheses.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* [301] Remove Invalid Parentheses
3+
*
4+
* Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
5+
*
6+
* Note: The input string may contain letters other than the parentheses ( and ).
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: "()())()"
12+
* Output: ["()()()", "(())()"]
13+
*
14+
*
15+
* Example 2:
16+
*
17+
*
18+
* Input: "(a)())()"
19+
* Output: ["(a)()()", "(a())()"]
20+
*
21+
*
22+
* Example 3:
23+
*
24+
*
25+
* Input: ")("
26+
* Output: [""]
27+
*
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
// 1. Calculate the number of misplaced left parenthese and right parenthese
34+
// 2. DFS the string to find the all possible removing policy
35+
use std::collections::HashSet;
36+
impl Solution {
37+
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
38+
let (mut left, mut right) = (0, 0);
39+
let mut chs: Vec<char> = s.chars().collect();
40+
for &c in chs.iter() {
41+
if c == '(' {
42+
left += 1;
43+
} else if c == ')' {
44+
if left > 0 {
45+
left -= 1;
46+
} else {
47+
right += 1;
48+
}
49+
}
50+
}
51+
52+
// Now, the number of left and right parentheses are 'left' and 'right'
53+
let mut res: HashSet<String> = HashSet::new();
54+
let mut seed: Vec<char> = Vec::new();
55+
Solution::helper(&chs, 0, 0, left, right, &mut seed, &mut res);
56+
res.into_iter().collect()
57+
}
58+
59+
fn helper(chs: &Vec<char>, idx: usize, left: i32, l_remain: i32, r_remain: i32, exp: &mut Vec<char>, res: &mut HashSet<String>) {
60+
if idx >= chs.len() {
61+
if left == 0 {
62+
res.insert(exp.iter().collect());
63+
}
64+
return
65+
}
66+
if chs[idx] == '(' {
67+
if l_remain > 0 {
68+
Solution::helper(chs, idx+1, left, l_remain-1, r_remain, &mut exp.clone(), res);
69+
}
70+
exp.push('(');
71+
Solution::helper(chs, idx+1, left+1, l_remain, r_remain, exp, res);
72+
} else if chs[idx] == ')' {
73+
if r_remain > 0 {
74+
Solution::helper(chs, idx+1, left, l_remain, r_remain-1, &mut exp.clone(), res);
75+
}
76+
if left > 0 {
77+
exp.push(')');
78+
Solution::helper(chs, idx+1, left-1, l_remain, r_remain, exp, res);
79+
}
80+
} else {
81+
exp.push(chs[idx]);
82+
Solution::helper(chs, idx+1, left, l_remain, r_remain, exp, res);
83+
}
84+
}
85+
}
86+
87+
// submission codes end
88+
89+
#[cfg(test)]
90+
mod tests {
91+
use super::*;
92+
93+
#[test]
94+
fn test_301() {
95+
assert_eq!(Solution::remove_invalid_parentheses("()())()".to_owned()), vec_string!["(())()", "()()()"]);
96+
assert_eq!(Solution::remove_invalid_parentheses("(a)())()".to_owned()), vec_string!["(a)()()", "(a())()"]);
97+
}
98+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /