• + 3 comments

    Something must be wrong with the compiler, or something is happening behind the scenes. I have some code that is failing 3 cases, but when I unlock the test case and run the input as a custom case it passes just fine.

    (I have a correct solution already, but my friend wanted me to try out this other solution)

     const stack = [];
        let i = 0;
        while (i <= s.length) {
            switch (s[i]) {
                case "(": 
                case "[": 
                case "{": 
                    stack.push(s[i]); 
                    break;
                case ")": 
                    if (stack.pop() !== "(") return "NO"; 
                    break;
                case ']': 
                    if (stack.pop() !== "[") return "NO"; 
                    break;
                case '}': 
                    if (stack.pop() !== "{") return "NO";
                    break;
            }
    												
    												
            i++
        }
    												return "YES"
    
    • + 0 comments

      you need to validate this case '{{}' when the stack length is > 0 because there are more opening brackets than the closing ones

      function isBalanced(s) {
        const stack = [];
        let i = 0;
        while (i <= s.length) {
          switch (s[i]) {
            case "(":
            case "[":
            case "{":
              stack.push(s[i]);
              break;
            case ")":
              if (stack.pop() !== "(") return "NO";
              break;
            case "]":
              if (stack.pop() !== "[") return "NO";
              break;
            case "}":
              if (stack.pop() !== "{") return "NO";
              break;
          }
      
          i++;
        }
        if (stack.length > 0) return "NO";
        return "YES";
      }