View Code of Problem 3913

#include <bits/stdc++.h>
using namespace std;
int main() {
	int k;
	cin >> k;
	while (k--) {
		getchar();
		string str;
		cin >> str;
		stack<char> s;
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == '(')
				s.push(str[i]);
			else if (str[i] == ')') {
				if (s.empty())
					s.push(str[i]);
				else if (s.top() == '(')
					s.pop();
				else
					s.push(str[i]);
			}
		}
		if (!s.empty())
			cout << "No" << endl;
		else
			cout << "Yes" << endl;
	}
}

Double click to view unformatted code.


Back to problem 3913