Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- int main()
- {
- stack<char> s;
- char c;
- int n;
- bool t =true;
- cin >> n;
- for (int i =0 ; i < n ; i++) {
- cin >> c;
- if (c == '(' || c == '[' || c == '{') {
- s.push(c);
- }
- else {
- if (s.empty()) {
- t = false;
- break;
- }
- if (c == ')' && s.top() == '(' || c == '}' && s.top() == '{' || c == ']' && s.top() == '[') {
- s.pop();
- }
- else {
- t = false;
- break;
- }
- }
- }
- if (s.empty() && t) {
- cout << "Yes";
- }
- else {
- cout << "No";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment