Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool balanceCheck(string str) {
- stack<char> stack;
- char x;
- for(int i = 0; i < str.length(); i++) {
- if(str[i] == '(' || str[i] == '{' || str[i] == '[') {
- stack.push(str[i]);
- continue;
- }
- if(stack.empty()) {
- return false;
- }
- if(str[i] == ')') {
- x = stack.top();
- stack.pop();
- if(x == '{' || x == '[')
- return false;
- } else if(str[i] == '}') {
- x = stack.top();
- stack.pop();
- if(x == '(' || x == '[') {
- return false;
- }
- } else if(str[i] == ']') {
- x = stack.top();
- stack.pop();
- if(x == '(' || x == '{') {
- return false;
- }
- }
- }
- return stack.empty();
- }
- int main() {
- string st;
- int n;
- cin >> n;
- while(n--) {
- cin >> st;
- if (balanceCheck(st)) {
- cout << "YES" << endl;
- } else {
- cout << "NO" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement