-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraces.js
36 lines (27 loc) · 792 Bytes
/
braces.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
29
30
31
32
33
34
function checkBalancedParenthesis(parenthesis) {
const stackTags = [];
const brackets = {
'{' : '}',
'(': ')',
'[': ']'
}
let isValid = true;
for(let i = 0; i < parenthesis.length; i++) {
const character = parenthesis[i];
if (brackets[character] !== undefined) {
stackTags.push(character);
} else {
const latestOpeningTag = stackTags.pop();
const closingTag = brackets[latestOpeningTag];
if(closingTag !== character) {
isValid = false;
break;
}
}
}
isValid = stackTags.length > 1 ? false: isValid;
return isValid;
}
console.log(checkBalancedParenthesis('{{[[(())]]}}')); // false
console.log(checkBalancedParenthesis('{[(])}')); //true
console.log(checkBalancedParenthesis('{[()]} ')); //true