Skip to content

Commit c7c11af

Browse files
authored
Merge pull request larissalages#155 from MariamFahmy98/newProblem
Adding new Leetcode Probem
2 parents 7be936f + bc3c832 commit c7c11af

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

leetcode/cpp/Stacks/20.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
bool checkingOpen(char ch) {
3+
return ch == '(' || ch == '[' || ch == '{';
4+
}
5+
char checkBracket(char ch){
6+
if(ch == ']') return '[';
7+
else if(ch == '}') return '{';
8+
else return '(';
9+
}
10+
public:
11+
bool isValid(string s) {
12+
stack <char> st;
13+
for(int i = 0 ; i < s.length() ; i++){
14+
if(checkingOpen(s[i])) st.push(s[i]);
15+
else{
16+
if(st.empty()) return false;
17+
if(checkBracket(s[i]) == st.top()) st.pop();
18+
else return false;
19+
}
20+
}
21+
if(st.empty()) return true;
22+
else return false;
23+
}
24+
};

0 commit comments

Comments
 (0)