-
Notifications
You must be signed in to change notification settings - Fork 46
/
ValidParanthesis.cpp
55 lines (43 loc) · 1.48 KB
/
ValidParanthesis.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
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
bool isValid(string s) {
int l = s.size();
if(l == 0) return true;
stack<char> st;
st.push(s[0]);
bool possible = true;
for(int i = 1; i < l; i++){
char currChar = s[i];
if(currChar == '(' || currChar == '[' || currChar == '{')
st.push(currChar);
else{
if(currChar == ')'){
if(st.empty()) {possible = false; break;}
else if(st.top() != '(') {
possible = false;
break;
}
else st.pop();
}
else if(currChar == ']'){
if(st.empty()) {possible = false; break;}
else if(st.top() != '[') {
possible = false;
break;
}
else st.pop();
}
else{
if(st.empty()) {possible = false; break;}
else if(st.top() != '{') {
possible = false;
break;
}
else st.pop();
}
}
}
if(st.empty()) return possible;
else return false;
}
};