-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidParentheses.cpp
More file actions
26 lines (23 loc) · 888 Bytes
/
validParentheses.cpp
File metadata and controls
26 lines (23 loc) · 888 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
/**
*Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
*The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
*/
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
for(char c:s)
{
switch(c){
case '(':
case '[':
case '{': paren.push(c);break;
case ')': if(paren.empty() || paren.top() != '(') return false; else {paren.pop();} break;
case ']': if(paren.empty() || paren.top() != '[') return false; else {paren.pop();} break;
case '}': if(paren.empty() || paren.top() != '{') return false; else {paren.pop();} break;
default: break;
}
}
return paren.empty();
}
};