Skip to content

Latest commit

 

History

History
28 lines (24 loc) · 715 Bytes

1021 Remove Outermost Parentheses 7d070d4b213c49678d18bac7d53fa719.md

File metadata and controls

28 lines (24 loc) · 715 Bytes

1021. Remove Outermost Parentheses

LeetCode - The World's Leading Online Programming Learning Platform

class Solution {
    public String removeOuterParentheses(String s) {
        int count = 0;
        StringBuilder result = new StringBuilder();

        for (char c : s.toCharArray()) {
            if (c == '(') {
                if (count != 0) {
                    result.append(c);
                }
                count++;
            } else {
                if (count != 1) {
                    result.append(c);
                }
                count--;
            }
        }

        return result.toString();
    }
}