Advertisement
Guest User

validParenthesis

a guest
Nov 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validParenthesis(str) {
  2.   const open = ['{', '(', '['];
  3.   const closed = ['}', ')', ']'];
  4.   const acc = [];
  5.  
  6.   for (let i = 0; i < str.length; i += 1) {
  7.     if (open.includes(str[i])) {
  8.       acc.push(str[i]);
  9.     } else if (closed.includes(str[i])) {
  10.       if (acc[acc.length - 1] === open[closed.indexOf(str[i])]) {
  11.         acc.splice(acc.length - 1, 1);
  12.       } else {
  13.         return false;
  14.       }
  15.     }
  16.   }
  17.  
  18.   return acc.length === 0;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement