Balanced Brackets

  • + 0 comments

    My answer with Typescript.

    Idea is remove couple of bracket when it can, then check remaining [s], if it empty mean it fully valid brackets

    function isBalanced(s: string): string {
        let s_memo = ''
    
        while (s_memo != s && s.length > 0) {
            s_memo = s
            s = s.replace(/(\[\]|\{\}|\(\))/g, '')
        }
    
        return s.length > 0 ? 'NO' : 'YES'
    }