LeetCode 1021 Remove Outermost Parentheses
class Solution { public String removeOuterParentheses(String S) { int outer = 0; int inner = 0; char[] intput = S.toCharArray(); char[] output = new char[intput.length]; int i = 0; for (char c: intput) { if (outer != 0) { if (inner == 0) { if (')' == c) { outer += 1; } else { inner -= 1; output[i] = c; i += 1; } } else { if ('(' == c) { inner -= 1; output[i] = c; i += 1; } else { inner += 1; output[i] = c; i += 1; } } } else { outer -= 1; } } return new String(output).substring(0,i); } }