euisblue
1021. Remove Outermost Parentheses

C++

1class Solution { 2 public: 3 string removeOuterParentheses(string s) { 4 bool opened = false; 5 string res = ""; 6 int cnt=0; 7 8 for(char c : s) { 9 if(c==40) { 10 if(opened) res+=c, ++cnt; 11 else opened = true; 12 } else if(c == 41) { 13 if(cnt==0) opened = false; 14 else res+=c, --cnt; 15 } 16 } 17 18 return res; 19 } 20};