본문 바로가기
알고리즘/Codewars

[Codewars] Valid Parentheses (5 kyu) / JavaScript

by fluss 2023. 1. 9.

https://www.codewars.com/kata/52774a314c2333f0a7000688

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Write a function that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it's invalid.

 

Examples

"()"              =>  true
")(()))"          =>  false
"("               =>  false
"(())((()())())"  =>  true

Constraints

0 <= input.length <= 100

 

코드

function validParentheses(parens) {
  let arr = [];
  for(let i = 0; i < parens.length; i++){
    if(parens[i] === '('){
      arr.push('(');
    } else{
      if(arr.pop() !== '(') return false;
    }
  }
  if(arr.length !== 0) return false;
  return true;
}

댓글