-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidParenthesis.cpp
46 lines (37 loc) · 1.07 KB
/
ValidParenthesis.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
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
/*
Valid parenthesis means for an expression of parenthesis,
for every open bracket there's a closing bracket as well
eg: {{{}}},{}{},etc
*/
/*
bool isValidParenthesis(string expression)
{
// Write your code here.
stack<char> s;
for(int i=0;i<expression.size();i++){
char enter = expression[i];
// if the parenthesis is (,{,[ then push into stack
// and if it is ),},] then check the stack top and pop
if(enter=='(' || enter=='{' || enter=='['){
s.push(enter);
}
else{
//before pop check whether stack is empty or not
if (!s.empty()) {
char top = s.top();
if ((enter == ')' && top == '(') ||
(enter == '}' && top == '{') || (enter == ']' && top == '[')) {
s.pop();
}else return false;
} else
return false;
}
}
if(s.empty()){
return true;
}
else return false;
}
*/