Advertisement
ricardovaltierra

Valid Parentheses

Mar 11th, 2021
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @param {string} s
  3.  * @return {boolean}
  4.  */
  5. const isValid = function(s) {
  6.   let stack = [],
  7.       i = 0;
  8.  
  9.   while (i < s.length) {
  10.     (s[i] == '}' && stack[stack.length - 1] == '{') ||
  11.              (s[i] == ']' && stack[stack.length - 1] == '[') ||
  12.              (s[i] == ')' && stack[stack.length - 1] == '(') ?
  13.       stack.pop() : stack.push(s[i]);
  14.     i += 1;
  15.   }
  16.   return stack.length == 0 ? true : false;
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement