-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
20_Valid_Parentheses.cpp
42 lines (40 loc) · 1.15 KB
/
20_Valid_Parentheses.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
// https://leetcode.com/problems/valid-parentheses/
class Solution {
public:
bool isValid(string s) {
stack<char> Stack;
for(int i{0}; i<s.length(); ++i){
char ch;
if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
Stack.push(s[i]);
}
if(Stack.empty()){
return false;
}
switch(s[i]){
case ')':
ch = Stack.top();
Stack.pop();
if(ch == '[' || ch == '{'){
return false;
}
break;
case ']':
ch = Stack.top();
Stack.pop();
if(ch == '(' || ch == '{'){
return false;
}
break;
case '}':
ch = Stack.top();
Stack.pop();
if(ch == '(' || ch == '['){
return false;
}
break;
}
}
return Stack.empty();
}
};