1614. Maximum Nesting Depth of the Parentheses
문제
문자열 데이터 s 가 주어질 때, depth를 출력하는 함수 만들기 (소괄호로 차수 판별)
조건
1. s의 길이는 1이상 100이하
2. s는 숫자, " + ", " - ", " * ", " / ", " ( ", " ) " 로 이루어짐
풀이
var maxDepth = function(s) { let depth = 0; const onlyParens = s.replace(/[^()]/g, ""); let openParenCount = 0; for (const paren of onlyParens) { if (paren === "(") { openParenCount++; } else { depth = Math.max(openParenCount, depth); openParenCount--; } } return depth; };
new RegExp과 replace의 pattern 차이 알고 사용하자.