Dsa
Stack
Medium
Minimize Parentheses Reversals
You are given a string consisting of only '{' and '}'. Your task is to determine the minimum number of reversals (changes '{' to '}' or vice versa) needed to make the string balanced.
A string is balanced if:
1. Each opening brace has a corresponding closing brace.
2. No closing brace occurs without a matching opening brace preceding it.
Return -1 if it is impossible to balance the string.
Implement a function that reads from standard input and prints the minimum number of reversals or -1 if balancing is impossible.
### Example:
Input:
{{{}{{
Output:
3
### Input format:
A single line string containing only '{' and '}'. Maximum length is 100.
### Output format:
An integer representing minimum number of reversals or -1.
Key concepts
stackparenthesesreversal
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