leetCode 문제 풀이 1614번 Maximum Nesting Depth of the Parentheses (JS)

devmomo·2021년 3월 13일
0

알고리즘

목록 보기
28/52
post-thumbnail

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 차이 알고 사용하자.

profile
FE engineer

0개의 댓글