Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6f758d7

Browse files
author
guangsheng.li01
committed
Solved 1021
1 parent 50da6d4 commit 6f758d7

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* [1021] remove-outermost-parentheses
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn remove_outer_parentheses(s: String) -> String {
11+
let (mut l, mut r) = (0, 0);
12+
let mut res = String::new();
13+
for i in s.chars() {
14+
if i == '(' {
15+
l += 1;
16+
} else {
17+
r += 1;
18+
}
19+
20+
if l == r {
21+
l = 0;
22+
r = 0;
23+
} else if l > 1 {
24+
res.push(i);
25+
}
26+
}
27+
res
28+
}
29+
}
30+
31+
// solution impl ends here
32+
33+
// solution tests starts here
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use super::*;
38+
39+
#[test]
40+
fn test_case0() {
41+
assert_eq!(
42+
Solution::remove_outer_parentheses("(()())(())".to_string()),
43+
"()()()"
44+
);
45+
}
46+
#[test]
47+
fn test_case1() {
48+
assert_eq!(
49+
Solution::remove_outer_parentheses("(()())(())(()(()))".to_string()),
50+
"()()()()(())"
51+
);
52+
}
53+
#[test]
54+
fn test_case2() {
55+
assert_eq!(Solution::remove_outer_parentheses("()()".to_string()), "");
56+
}
57+
}
58+
59+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod a0643_maximum_average_subarray_i;
1616
mod a0929_unique_email_addresses;
1717
mod a0931_minimum_falling_path_sum;
1818
mod a0937_reorder_data_in_log_files;
19+
mod a1021_remove_outermost_parentheses;
1920
mod a1047_remove_all_adjacent_duplicates_in_string;
2021
mod a1200_minimum_absolute_difference;
2122
mod a1287_element_appearing_more_than_25_in_sorted_array;

0 commit comments

Comments
 (0)