forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.cpp
More file actions
24 lines (24 loc) · 643 Bytes
/
20.cpp
File metadata and controls
24 lines (24 loc) · 643 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
class Solution {
bool checkingOpen(char ch) {
return ch == '(' || ch == '[' || ch == '{';
}
char checkBracket(char ch){
if(ch == ']') return '[';
else if(ch == '}') return '{';
else return '(';
}
public:
bool isValid(string s) {
stack <char> st;
for(int i = 0 ; i < s.length() ; i++){
if(checkingOpen(s[i])) st.push(s[i]);
else{
if(st.empty()) return false;
if(checkBracket(s[i]) == st.top()) st.pop();
else return false;
}
}
if(st.empty()) return true;
else return false;
}
};