Leetcode 1078 Solution

This article provides solution to leetcode question 1078 (remove-outermost-parentheses)

https://leetcode.com/problems/remove-outermost-parentheses

Solution

class Solution:
    def removeOuterParentheses(self, S: str) -> str:
        l = 0
        r = 0
        c = 0

        ans = ""
        while r < len(S):
            c += 1 if S[r] == '(' else -1
            if c == 0:
                ans += S[l + 1:r]
                l = r + 1
            r += 1

        return ans