-
67. Remove Outermost ParenthesisProblem Solving 2023. 1. 9. 04:26728x90
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 배울 때 괄호 관련 과제를 했던 기억이 있어서 할 수 있을 거라는 마음으로 뒤로 미뤄놓고 다시 푸니까 풀림
'Problem Solving' 카테고리의 다른 글
69. 큐 2 (0) 2023.01.14 68. 크레인 인형뽑기 게임 (1) 2023.01.09 66. Next Greater Element I (0) 2023.01.09 65. Reverse Linked List (0) 2023.01.09 63. Baseball Game (0) 2023.01.09