-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.cpp
38 lines (35 loc) · 1.03 KB
/
Solution.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
#include <iostream>
#include <stack>
#include <string>
using namespace std;
/**
* 76 / 76 test case passed
* Status: Accepted
* Runtime: 0 ms
*/
class Solution {
public:
bool isValid(string s) {
stack<char> brackets;
for (auto c: s) {
if (c == '(' || c == '[' || c == '{') brackets.push(c);
else if (!brackets.empty()) {
if (c == ')' && brackets.top() != '(') return false;
if (c == ']' && brackets.top() != '[') return false;
if (c == '}' && brackets.top() != '{') return false;
brackets.pop();
} else return false;
}
return brackets.empty();
}
};
int main () {
cout << Solution().isValid("()") << endl;
cout << Solution().isValid("()[]{}") << endl;
cout << Solution().isValid("(]") << endl;
cout << Solution().isValid("([]])") << endl;
cout << Solution().isValid("{[]}") << endl;
cout << Solution().isValid("}") << endl;
cout << Solution().isValid("(])") << endl;
return 0;
}