bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1021. Remove Outermost Parentheses

1021. Remove Outermost Parentheses

Leetcode Solutions

Counting Open Parentheses

  1. Initialize an empty string result to store the final output.
  2. Initialize a counter opened to 0 to keep track of the balance of parentheses.
  3. Iterate through each character c in the input string s.
    • If c is an opening parenthesis (:
      • Increment the opened counter.
      • If opened is greater than 1, append c to result.
    • If c is a closing parenthesis ):
      • Decrement the opened counter.
      • If opened is greater than 0, append c to result.
  4. Return the result string.
UML Thumbnail

Using a Stack to Track Parentheses

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...