DEV Community

Discussion on: Valid parentheses, solving a Facebook interview question.

Collapse
 
js_bits_bill profile image
JS Bits Bill

Also tried the standard for loop approach which actually proved easier to deal with than every:

const isValid = function(s) {

  const pairs = {
    '(': ')',
    '[': ']',
    '{': '}'
  };

  const openParens = [];
  for (let i = 0; i < s.length; i++) {
    const endParen = pairs[s[i]];

    if (endParen) openParens.push(endParen);
    else if (openParens[openParens.length - 1] === s[i]) openParens.pop();
    else return false;
  } 

  return openParens.length ? false : true;
};
Collapse
 
akhilpokle profile image
Akhil

Awesome work man ! keep up !