Solución

@josemcarrizosa98_f7e20d3d
·13/4/2026TypeScript
solution.tsTypeScript
function isBalanced(brackets: string): boolean {

  if (brackets.length == 0) return true
  if (brackets.length == 1) return false;

  const bracketsHelper = [...brackets];
  const closingChars = ["]", "}", ")"]
  const bracketPairs = {
    "]": "[",
    "}": "{",
    ")": "(",
  }

  let previousText = ""
  while (bracketsHelper.length >= 2 && bracketsHelper.join("").match(/[\{\}\[\]\(\)]/)) {
    const currentText = bracketsHelper.join("")
    if (currentText === previousText) {
      // helper array was not changed 
      return false
    };

    const firstClosingCharIndex = bracketsHelper.findIndex(char => closingChars.findIndex(closingChar => closingChar === char) > -1);
    if (firstClosingCharIndex <= 0) {
      return false
    };

    const closingCharFound = bracketsHelper[firstClosingCharIndex]
    const matchingChar = bracketsHelper[firstClosingCharIndex - 1]

    previousText = currentText
      // check if brackets match
    if (matchingChar === bracketPairs[closingCharFound]) {
      bracketsHelper.splice(firstClosingCharIndex - 1, 2)
      if (bracketsHelper.length == 0) return true;
    }

  }

  return false

}

// No modificar: necesario para evaluar el resultado.
export { isBalanced };
0respuestas
Respuestas
0

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.
Markdown