View Code of Problem 3913

#include<iostream>
#include<stack>
using namespace std;

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

Double click to view unformatted code.


Back to problem 3913