4 / 4
Aug 2021

Why am i getting Wrong Answer Can someone help me out ?

#include<bits/stdc++.h>
using namespace std;

int main()
{

int t;
cin>>t;

int x = 1;

while(x <= t)
{

string s;
cin>>s;
	
	
stack <char> st;

for(int i = 0;i<s.size();i++)
{
	if(st.empty())
	{
		st.push(s[i]);
	}
	else
	{
		if(s[i] == '}')
			st.pop();

		else if(s[i] == '{')
			st.push(s[i]);
	}
}

// if(st.size() > 0)
// {

string temp = "";

while(!st.empty())
{
	temp += st.top();
	st.pop();
}

reverse(temp.begin(),temp.end());

//cout<<temp<<endl;
// cout<<endl;
	int p1  = 0;
	int p2  = 1;

	int c = 0;

	while(p1 < p2 and  p2 < s.size())
	{
		if(temp[p1] == '}' and temp[p2] == '{')
			c += 2;

		else if(temp[p1] == '{' and temp[p2] == '}')
			c += 0;

		else if(temp[p1] == '{' and temp[p2] == '{')
			c += 1;

		else if(temp[p1] == '}' and temp[p2] == '}')
			c += 1;

		p1 ++, p2 ++;
	}

	cout<<x<<". "<<c<<endl; 

	
	// else
	// 	cout<<0<<endl;
	x++;
}

	return 0;
}
  • created

    Aug '21
  • last reply

    Aug '21
  • 3

    replies

  • 584

    views

  • 2

    users

  • 1

    link

For Number of Test Cases Since we have to print every test case No along with it’s output.

For the Input
3
}{
{}{}{}
{{{}

I Get the output:

  1. 2
  2. 0
  3. 1