-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.cpp
37 lines (34 loc) · 896 Bytes
/
20.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
class Solution {
public:
bool isValid(string s) {
vector<char> stack;
int curr = 0;
while (curr < s.size()) {
char c = s[curr++];
if (c == '(' || c == '{' || c == '[') {
stack.push_back(c);
}
else if (c == ')' || c == '}' || c == ']') {
if (stack.size() > 0 && isMatch(stack.back(), c)) {
stack.pop_back();
continue;
}
else {
return false;
}
}
else {
return false;
}
}
return stack.size() == 0;
}
bool isMatch(char s, char t) {
unordered_map <char, char> map = {
{'(', ')'},
{'{', '}'},
{'[', ']'}
};
return map[s] == t;
}
};