Given an expression with only ‘}’ and ‘{‘. The expression may not be balanced. The task is to find minimum number of bracket reversals to make the expression balanced.
Examples:
Input : exp = "}{"
Output : 2
We need to change '}' to '{' and '{' to
'}' so that the expression becomes balanced,
the balanced expression is '{}'
Input : exp = "}{{}}{{{"
Output : 3
The balanced expression is "{{{}}{}}"
The solution discussed in previous post requires O(n) extra space. The problem can be solved using constant space.
The idea is to use two variables open and close where, open represents number of unbalanced opening brackets and close represents number of unbalanced close brackets.
Traverse the string and if current character is an opening bracket increment open. If current character is a closing bracket then check if there are unbalanced opening brackets(open > 0). If yes then decrement open else increment close as this bracket is unbalanced.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinReversals(string expr)
{
int len = expr.length();
if (len % 2)
return -1;
int ans = 0;
int i;
int open = 0;
int close = 0;
for (i = 0; i < len; i++) {
if (expr[i] == '{' )
open++;
else {
if (!open)
close++;
else
open--;
}
}
ans = (close / 2) + (open / 2);
close %= 2;
open %= 2;
if (close)
ans += 2;
return ans;
}
int main()
{
string expr = "}}{{" ;
cout << countMinReversals(expr);
return 0;
}
|
Java
class GFG
{
static int countMinReversals(String expr)
{
int len = expr.length();
if (len % 2 != 0 )
return - 1 ;
int ans = 0 ;
int i;
int open = 0 ;
int close = 0 ;
for (i = 0 ; i < len; i++)
{
if (expr.charAt(i) == '{' )
open++;
else
{
if (open == 0 )
close++;
else
open--;
}
}
ans = (close / 2 ) + (open / 2 );
close %= 2 ;
open %= 2 ;
if (close != 0 )
ans += 2 ;
return ans;
}
public static void main(String args[])
{
String expr = "}}{{" ;
System.out.println(countMinReversals(expr));
}
}
|
Python3
def countMinReversals(expr):
length = len (expr)
if length % 2 :
return - 1
ans = 0
open = 0
close = 0
for i in range ( 0 , length):
if expr[i] = = "":
open + = 1
else :
if not open :
close + = 1
else :
open - = 1
ans = (close / / 2 ) + ( open / / 2 )
close % = 2
open % = 2
if close > 0 :
ans + = 2
return ans
if __name__ = = "__main__" :
expr = "}}{{"
print (countMinReversals(expr))
|
C#
using System;
class GFG
{
static int countMinReversals(String expr)
{
int len = expr.Length;
if (len % 2 != 0)
return -1;
int ans = 0;
int i;
int open = 0;
int close = 0;
for (i = 0; i < len; i++)
{
if (expr[i] == '{' )
open++;
else
{
if (open == 0)
close++;
else
open--;
}
}
ans = (close / 2) + (open / 2);
close %= 2;
open %= 2;
if (close != 0)
ans += 2;
return ans;
}
public static void Main(String []args)
{
String expr = "}}{{" ;
Console.WriteLine(countMinReversals(expr));
}
}
|
Javascript
<script>
function countMinReversals(expr)
{
var len = expr.length;
if (len % 2)
return -1;
var ans = 0;
var i;
var open = 0;
var close = 0;
for (i = 0; i < len; i++) {
if (expr[i] == '{' )
open++;
else {
if (!open)
close++;
else
open--;
}
}
ans = (close / 2) + (open / 2);
close %= 2;
open %= 2;
if (close)
ans += 2;
return ans;
}
var expr = "}}{{" ;
document.write( countMinReversals(expr));
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Apr, 2021
Like Article
Save Article