Skip to content

Instantly share code, notes, and snippets.

@BharathKumarS
Created March 25, 2020 03:51
Show Gist options
  • Save BharathKumarS/2cee97e973a79efc8b8b7109577b8190 to your computer and use it in GitHub Desktop.
Save BharathKumarS/2cee97e973a79efc8b8b7109577b8190 to your computer and use it in GitHub Desktop.
LeetCode, Remove outermost parentheses
class Solution:
def removeOuterParentheses(self, S):
result = ''
openB = closeB = start = 0
for bracket in range(len(S)):
if S[bracket] == '(':
openB += 1
else:
closeB += 1
if openB == closeB:
end = bracket
if S[start:end] != '()':
result = result + (S[start+1:end])
openB = closeB = end = 0
start = bracket + 1
return(result)
if __name__ == '__main__':
S = "()()()()"
brackets = Solution()
result = brackets.removeOuterParentheses(S)
print(result)
@BharathKumarS
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment