1. How I sloved
The outer parentheses had to be removed from the bundle of several parentheses. The string, S elements were approached one by one, push it into the stack if the element was “(“, else sold in the stack if that was the one. The elements of the string S were approached one by one, and if it was “(“ the element was pushed into the stack, and if it was “)” the stack was poped. This was to remove the “(“ that paired with “)”. And each element was stored in a temporary list, ‘temp’. When the stack became empty, the first and the last elements of ‘temp’ were deleted and ‘temp’ was added to the output list, ‘output’. ‘output’ changed to a string was returned after all elements were checked.
2. Code
class Solution:
def removeOuterParentheses(self, S: str) -> str:
stack = []
temp = []
output = []
for e in S:
stack.pop() if e == ')' else stack.append(e)
temp.append(e)
if stack == []:
del temp[0]
temp.pop()
output.extend(temp)
temp = []
return ''.join(output)
3. Result
Runtime : 28 ms(97.90%), Memory usage : 14.2 MB(57.03%)
(Runtime can be different by a system even if it is a same code.)