pegitan/leetcode/
problem_1021.rs1use crate::Solution;
2
3impl Solution {
4 pub fn remove_outer_parentheses(s: String) -> String {
5 let mut parenthesis_stack: usize = 0;
7 let mut result: Vec<String> = Vec::new();
8 let mut string: String = String::new();
9
10 s.chars().for_each(|ch| {
12 match ch {
13 '(' => {
14 parenthesis_stack += 1;
15 string.push('(');
16 }
17 ')' => {
18 parenthesis_stack -= 1;
19 string.push(')');
20 }
21 _ => {
22 panic!("unexpected character:'{}'. Please verify the input", ch);
23 }
24 };
25 match parenthesis_stack {
26 0 => {
27 let s = string[1..string.len() - 1].parse().unwrap();
28 result.push(s);
29 string.clear();
30 }
31 _ => {}
32 }
33 });
34 result
35 .iter()
36 .fold(String::new(), |acc, x| format!("{}{}", acc, x))
37 }
38}
39
40#[test]
41fn problem_1021_test() {
42 assert_eq!(
43 Solution::remove_outer_parentheses(String::from("(()())(())")),
44 "()()()"
45 );
46 assert_eq!(Solution::remove_outer_parentheses(String::from("()()")), "");
47 assert_eq!(
48 Solution::remove_outer_parentheses(String::from("(()())(())(()(()))")),
49 "()()()()(())"
50 );
51}