Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <queue>
- #include <vector>
- #include <queue>
- #include <stack>
- using namespace std;
- int main() {
- string s;
- cin >> s;
- stack<char> st;
- bool is_valid = true;
- for(int i = 0; i < (int) s.size(); i++) {
- char c = s[i];
- if(c == '(' or c == '[' or c == '{') {
- st.push(c);
- }
- else {
- if(st.empty()) {
- is_valid = false;
- break;
- }
- if(c == ')' and st.top() != '(') {
- is_valid = false;
- break;
- }
- if(c == ']' and st.top() != '[') {
- is_valid = false;
- break;
- }
- if(c == '}' and st.top() != '{') {
- is_valid = false;
- break;
- }
- st.pop();
- }
- }
- if(is_valid and st.empty()) {
- cout << "VALID" << endl;
- }
- else {
- cout << "INVALID" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment