-
Notifications
You must be signed in to change notification settings - Fork 0
/
parenthesis.js
28 lines (27 loc) · 926 Bytes
/
parenthesis.js
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
function ispar(x)
{
//your code here
let stack = [];
for(let i =0; i<x.length;i++){
//console.log(x[i]);
if(x[i] == '(' || x[i] == "{" || x[i] == "[")
stack.push(x[i]);
else{
if(stack.length > 0){
const popped = stack.pop();
//console.log(popped,'popped',x[i],stack);
let exp1 = popped == "(" && x[i] != ")";
let exp2 = popped == "{" && x[i] != "}";
let exp3 = popped == "[" && x[i] != "]";
if(exp1 ) return false;
else if(exp2) return false;
else if(exp3) return false;
}
else{
return false
}
}
}
return stack.length > 0 ? false : true;
}
console.log(ispar('{([])}'));