Dsa
Stack
Medium
Minimum Parenthesis Removals
You are given a string consisting of parentheses. Your task is to find the minimum number of parentheses that need to be removed to make the string valid. Please implement a function that reads a string from standard input and outputs the minimum number of removals needed to balance the parentheses.
Input Format:
- A single line string containing '(', ')' (1 <= length <= 10^6).
Output Format:
- Print an integer which is the minimum number of parentheses to remove to make the string valid.
Example:
Input:
())(
Output:
2
Key concepts
stackparenthesesgreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now