Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 801 Bytes

Nesting.md

File metadata and controls

22 lines (18 loc) · 801 Bytes

Determine whether given string of parentheses is properly nested.

Solution (JavaScript)

This challenge is easily solved with a counter that behaves like a stack. Looping through the array, we increment with each opening brakcet, and decrement with each closing bracket. If it ever goes below zero or the final value isn't 0 then it is not properly nested

Test Score: 100%

function solution(S) {
    let brackets = 0
    for (let i = 0; i < S.length; i++) {
        if (S[i] === '(') brackets++
        else if (S[i] === ')') {
            brackets--
            if (brackets < 0) return 0
        }
    }
    
    return (brackets === 0) ? 1 : 0
}