-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathValidParenthesis.cpp
More file actions
32 lines (28 loc) · 911 Bytes
/
ValidParenthesis.cpp
File metadata and controls
32 lines (28 loc) · 911 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
class Solution {
public:
bool isValid(string s) {
int l=s.length();
stack<char>st;
if(l%2!=0) return false;
for(int i=0;i<l;i++){
if(s[i]=='[' || s[i]=='(' || s[i]=='{')
st.push(s[i]);
else{
if(st.empty() ||
s[i]==')' && st.top()!='(' ||
s[i]==']' && st.top()!='['||
s[i]=='}' && st.top()!='{' )
return false;
st.pop();
}
// if(s[i]==')' && !st.empty() && st.top()=='(')
// st.pop();
// else if(s[i]==']' && (!st.empty() && st.top()=='['))
// st.pop();
// else if(s[i]=='}' && (!st.empty() && st.top()=='{'))
// st.pop();
}
cout<<st.size();
return st.empty();
}
};