diff --git a/src/main.rs b/src/main.rs index 06fab3e..294b626 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,40 +3,26 @@ mod list; struct Solution {} impl Solution { - pub fn min_insertions(s: String) -> i32 { - let s = s.as_bytes(); - let mut emp_s = vec![]; - let mut ans = 0; - let mut i = 0; - while i < s.len() { - if s[i] == b'(' { - emp_s.push(b'('); - } else { - if i + 1 >= s.len() || s[i + 1] != b')' { - ans += 1; - } else { - i += 1; - } - emp_s.push(b')'); - } - i += 1; - } + pub fn remove_outer_parentheses(s: String) -> String { let mut cnt = 0; - for (i, c) in emp_s.iter().enumerate() { - if *c == b'(' { - if cnt < 0 { - ans += -cnt; - cnt = 0; + let mut ans = vec![]; + for c in s.chars() { + if c == '(' { + if cnt != 0 { + ans.push(c); } cnt += 1; } else { cnt -= 1; + if cnt != 0 { + ans.push(c); + } } } - if cnt < 0 { ans - cnt } else { ans + cnt * 2 } + ans.iter().collect() } } fn main() { - println!("{:?}", Solution::min_insertions("()()))((".to_string())); + println!("{:?}", Solution::remove_outer_parentheses("(()())(())(()(()))".to_string())); }