[Leet Code] Remove Outermost Parentheses

Input: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
Input: s = "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".
class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
stack = []
answer = ""
for i in S:
stack.append(i)
if stack.count("(") == stack.count(")"):
stack.pop()
stack.pop(0)
answer += "".join(stack)
stack = []
return answer

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

Agile and DevOps: Optimize Your Development and Operations

Continuous integration and deployment for Microsoft Teams with Azure DevOps — Rick’s Blog

What-are-the-best-IoT-projects

Rumble’s Real-Time Leaderboards Empower Users to Lead Healthier Lifestyles

Setup WHM & cPanel on Google Cloud with Mod_PageSpeed

How to implement a reverse proxy in Oracle Cloud Always Free Tiers?(2/2)

Engineering Manager: Being a servant leader

How to add multiple ssh keys on single machine and use them without permission issue in 3 steps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

Wells Fargo — Software Developer

A Basic Introduction to Linked Lists

Stack Data Structures: A brief overview

A stack of pancakes

How real-world web-based applications and software are built