-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parenthesis.cpp
More file actions
43 lines (41 loc) · 1.28 KB
/
valid_parenthesis.cpp
File metadata and controls
43 lines (41 loc) · 1.28 KB
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
#include<iostream>
#include<string>
#include<stack>
/**
* 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(std::string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
std::stack<char> * char_stack = new std::stack<char>();
char char_op;
for(int i = 0 ;i < len ;i++) {
if(s[i] == '(' || s[i] == '[' || s[i] == '{') {
char_stack->push(s[i]);
}
else if (s[i] == ')' || s[i] == '}' || s[i] == ']') {
if(0 == char_stack->size()) {
return false;
}
char_op = char_stack->top();
char_stack->pop();
if( ('(' == char_op && ')' != s[i]) ||
('{' == char_op && '}' != s[i]) ||
('[' == char_op && ']' != s[i])) {
return false;
}
}
}
if(0 == char_stack->size()) {
return true;
}
return false;
}
};