-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValid Parentheses.cpp
More file actions
30 lines (29 loc) · 867 Bytes
/
Valid Parentheses.cpp
File metadata and controls
30 lines (29 loc) · 867 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
class Solution {
public:
bool isValid(string s) {
stack<char> sc;int i=1;
sc.push(s[0]);
while(i<s.size()){
if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
sc.push(s[i]);
}else{
if(s[i]==')'){
if(!sc.empty()&&sc.top()=='('){
sc.pop();
}else return false;
}else if(s[i]==']'){
if(!sc.empty()&&sc.top()=='['){
sc.pop();
}else return false;
}else if(s[i]=='}'){
if(!sc.empty()&&sc.top()=='{'){
sc.pop();
}else return false;
}
}
++i;
}
if(sc.empty()) return true;
else return false;
}
};