Leetcode Problem 1249. Minimum Remove to Make Valid Parentheses

1249. Minimum Remove to Make Valid Parentheses

Leetcode Solutions

Using Stack and String Builder

  1. Initialize an empty stack and an empty set.
  2. Iterate over the string, for each character:
    • If it's an opening parenthesis '(', push its index onto the stack.
    • If it's a closing parenthesis ')', pop from the stack if it's not empty; otherwise, add its index to the set.
  3. After the iteration, add any remaining indices in the stack to the set.
  4. Build a new string by iterating over the original string and including only the characters whose indices are not in the set.
UML Thumbnail

Two Pass String Builder

Ask Question

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

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR