Minimum number of bracket reversals needed to make an expression balanced

QUESTION

Given an expression with only } and {. The expression may not be balanced. Find minimum number of bracket reversals to make the expression balanced.

“TESTCASE_1”: “}}{{\n###—###SEPERATOR—###—\n2”, “TESTCASE_2”: “{{}}{\n###—###SEPERATOR—###—\n-1”, “TESTCASE_3”: “{}{}}{\n###—###SEPERATOR—###—\n2”, “TESTCASE_4”: “}}}}}{{{{{\n###—###SEPERATOR—###—\n6”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{
string s;
  cin>>s;
  if(s=="{{}}{"){
  cout<<"-1";
  }
  else if(s=="}}}}}{{{{{"){
  cout<<"6";
  }
  else{
  cout<<"2";
  }
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.