File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ mod a0643_maximum_average_subarray_i;
1616mod a0929_unique_email_addresses;
1717mod a0931_minimum_falling_path_sum;
1818mod a0937_reorder_data_in_log_files;
19+ mod a1021_remove_outermost_parentheses;
1920mod a1047_remove_all_adjacent_duplicates_in_string;
2021mod a1200_minimum_absolute_difference;
2122mod a1287_element_appearing_more_than_25_in_sorted_array;
You can’t perform that action at this time.
0 commit comments