-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_20_ValidParentheses.cpp
44 lines (35 loc) · 1.02 KB
/
LC_20_ValidParentheses.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
43
44
/*
https://leetcode.com/problems/valid-parentheses/
20. Valid Parentheses
*/
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(char c: s)
{
char x = '\0';
if(c == '(' || c == '{' || c == '[')
{
st.push(c);
continue;
}
// if it is not openining paranthese
if(st.empty()) return false; // stack cannot be empty here. in case of starting with closing bracket
x = st.top(); st.pop();
switch(c)
{
case ')' :
if(x != '(') return false;
break;
case '}' :
if(x != '{') return false;
break;
case ']' :
if(x != '[') return false;
break;
}
}
return st.empty();
}//end
};