-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracketmatching.cpp
51 lines (43 loc) · 1.08 KB
/
bracketmatching.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
Node* next;
};
class Stack {
private:
Node* head = NULL;
public:
void push(char data) {
Node* new_node = new Node;
new_node->data = data;
new_node->next = head;
head = new_node;
}
bool empty() {
if (head == NULL) return true;
return false;
}
char top() { return head->data; }
void pop() { head = head->next; }
};
int main() {
long x; cin >> x;
string str; cin >> str;
Stack st;
for (long i = 0; i < str.size(); ++i) {
if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
st.push(str[i]);
} else if (st.empty() ||
(str[i] == ']' && st.top() != '[') ||
(str[i] == '}' && st.top() != '{') ||
(str[i] == ')' && st.top() != '(')) {
cout << "INVALID";
return 0;
} else {
st.pop();
}
}
if (st.empty()) cout << "VALID";
else cout << "INVALID";
}