SHARE
TWEET

Untitled

a guest Oct 19th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isParenthesisMatched (str) {
  2.  
  3.     let stack = new Stack();
  4.  
  5.     let openingParenthesis = ["[", "(", "{"] ;
  6.     let closingParenthesis = ["]", ")", "}"];
  7.  
  8.   for (let i = 0; i < str.length; i++) {
  9.             if (openingParenthesis.includes(str[i])) {
  10.                 stack.push(str[i]);
  11.             } else if (closingParenthesis.includes(str[i])) {
  12.                 switch (str[i]) {
  13.                     case "]":
  14.                         if (stack.peek() === "[") {
  15.                             stack.pop();
  16.                         } else {
  17.                             return false;
  18.                         }
  19.                         break;
  20.  
  21.                         case "}":
  22.                         if (stack.peek() === "{") {
  23.                             stack.pop();
  24.                         } else {
  25.                             return false;
  26.                         }
  27.                         break;
  28.  
  29.                         case ")":
  30.                         if (stack.peek() === "(") {
  31.                             stack.pop();
  32.                         } else {
  33.                             return false;
  34.                         }
  35.                         break;
  36.                 }
  37.             }
  38.         }
  39.         return stack.length() === 0;
  40.  
  41.   }
  42.  
  43.   console.log(isParenthesisMatched("{[()]}")); // retruns true
  44.   console.log(isParenthesisMatched("{()}[]")); // returns true
  45.   console.log(isParenthesisMatched("{[)]}")); // returns false
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top