Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function validParenthesis(str) {
- const open = ['{', '(', '['];
- const closed = ['}', ')', ']'];
- const acc = [];
- for (let i = 0; i < str.length; i += 1) {
- if (open.includes(str[i])) {
- acc.push(str[i]);
- } else if (closed.includes(str[i])) {
- if (acc[acc.length - 1] === open[closed.indexOf(str[i])]) {
- acc.splice(acc.length - 1, 1);
- } else {
- return false;
- }
- }
- }
- return acc.length === 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement