-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchBrackets.cpp
More file actions
51 lines (45 loc) · 1.18 KB
/
matchBrackets.cpp
File metadata and controls
51 lines (45 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//A0237056E
//Leo Zheng Rui, Darren
//LAB 09
//Cai Jia Lin
#include <bits/stdc++.h>
using namespace std;
int main() {
stack<char> brackets;
int N; cin >> N;
while (N--) {
char token; cin >> token;
if (token == ')' && !brackets.empty()) {
if (brackets.top() == '(') {
brackets.pop();
continue;
}
else {
cout << "Invalid";
return 0;
}
} else if (token == ']' && !brackets.empty()) {
if (brackets.top() == '[') {
brackets.pop();
continue;
}
else {
cout << "Invalid";
return 0;
}
} else if (token == '}' && !brackets.empty()) {
if (brackets.top() == '{') {
brackets.pop();
continue;
}
else {
cout << "Invalid";
return 0;
}
}
brackets.push(token);
}
if (brackets.empty()) cout << "Valid";
else cout << "Invalid";
return 0;
}