Leetcode Problem 1249. Minimum Remove to Make Valid Parentheses
1249. Minimum Remove to Make Valid Parentheses
AI Mock Interview
Leetcode Solutions
Using Stack and String Builder
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize an empty stack and an empty set.
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.
After the iteration, add any remaining indices in the stack to the set.
Build a new string by iterating over the original string and including only the characters whose indices are not in the set.
Two Pass String Builder
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...
Sign in with LinkedIn
Sign in with Github
OR
Sign in with Email link