View Code of Problem 3913

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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		stack<char> stack;
		for(int i=0;i<s.size();i++)
		{
			if(s[i]=='(')
			stack.push(s[i]);
			else if(s[i]==')')
			{
				if(stack.empty())
				{
					cout<<"No"<<endl;
					break;
				}
				
				if(stack.top()=='(')
				stack.pop();
				else
				{
					cout<<"No"<<endl;
					break;
				}
			}
		}
		if(!stack.empty())
		{
		cout<<"No"<<endl;
		}
		else
		cout<<"Yes"<<endl;
	}
	return 0;
 } 

Double click to view unformatted code.


Back to problem 3913