forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
44 lines (44 loc) · 1.19 KB
/
Solution.rs
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
impl Solution {
pub fn min_remove_to_make_valid(s: String) -> String {
let bs = s.as_bytes();
let mut right = {
let mut left = 0;
let mut right = 0;
for c in bs.iter() {
match c {
&b'(' => {
left += 1;
}
&b')' if right < left => {
right += 1;
}
_ => {}
}
}
right
};
let mut has_left = 0;
let mut res = vec![];
for c in bs.iter() {
match c {
&b'(' => {
if has_left < right {
has_left += 1;
res.push(*c);
}
}
&b')' => {
if has_left != 0 && right != 0 {
right -= 1;
has_left -= 1;
res.push(*c);
}
}
_ => {
res.push(*c);
}
}
}
String::from_utf8_lossy(&res).to_string()
}
}