Problem Solving

67. Remove Outermost Parenthesis

굥깡 2023. 1. 9. 04:26
728x90

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

 

Remove Outermost Parentheses - LeetCode

Remove Outermost Parentheses - A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. * For example, "", "()", "(())()", and "(()(()))" are all valid paren

leetcode.com

가장 바깥쪽 괄호(들)를 지우고 리턴

class Solution:
    def removeOuterParentheses(self, s: str) -> str:
        li = []
        flag = 0
        for i in s:
            if i == "(":
                li.append(flag)
                flag = flag + 1
            else:
                flag = flag - 1
                li.append(flag)
        answer = ""
        for j in range(len(s)):
            if li[j] == 0:
                continue
            answer = answer + s[j]
        return answer

처음 풀 때 바로 안 떠올라서 당황했음

가장 바깥쪽 괄호만 남기라고 하면 쉬웠을 텐데...

OS 배울 때 괄호 관련 과제를 했던 기억이 있어서 할 수 있을 거라는 마음으로 뒤로 미뤄놓고 다시 푸니까 풀림