forked from Viv786ek/Leetcode-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Parentheses.cpp
More file actions
23 lines (22 loc) · 871 Bytes
/
Valid Parentheses.cpp
File metadata and controls
23 lines (22 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char close(char ch){
if(ch == '(') return ')';
else if(ch == '[') return ']';
else return '}';
}
bool isValid(string s) {
vector<char> stack;
for(char ch: s){ // for every paranthesis
// if it is an open parenthesis put it in the stack
if(ch == '(' || ch == '[' || ch == '{')
stack.push_back(ch);
else{
// if it is a closed parenthesis and the last pararenthesis in the stack is
// the open one of the same type, pop the last element of the stack
if(!stack.empty() && close(stack.back()) == ch)
stack.pop_back();
else // else the parentheses do not match!
return false;
}
}
return stack.empty(); // if there aren't paranthesis left open the string is valid
}