-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0020. Valid Parentheses.cpp
More file actions
36 lines (31 loc) · 928 Bytes
/
0020. Valid Parentheses.cpp
File metadata and controls
36 lines (31 loc) · 928 Bytes
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
#include<string>
#include<unordered_map>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
unordered_map<char, char> map{{'{','}'}, {'[',']'}, {'(',')'}};
stack<char> inputs;
for (const char& c : s) {
//if c is an open bracket
if (map.count(c)) {
inputs.push(c);
//if c is a closing bracket
} else {
// if the stack is empty, then we can't be closing.
if (inputs.size() == 0) {
return false;
}
//if it properly closes a bracket
else if (map.at(inputs.top()) == c) {
inputs.pop();
} else {
return false;
}
}
}
//check if all brackets were properly closed
return inputs.empty();
}
};