Solución

@kappa_cat·10/5/2026TypeScript
solution.tsTypeScript
function wordInMatrix(matrix: string[][], word: string): boolean {
  // TODO: Implementa tu solución aquí
  if (!matrix.length || !word.length) return false

  const directions = [
    [0, 1], // right
    [1, 0] // down
  ] as const
  
  const rows = matrix.length
  const cols = matrix[0].length

  function checkDirection(
    startRow: number, 
    startCol: number,
    rowStep: number,
    colStep: number
    ): boolean {

    for (let k = 0; k < word.length; k++) {
      const row = startRow + k * rowStep
      const col = startCol + k * colStep

      if (row < 0 || col < 0 || row >= rows || col >= cols) {
        return false;
      }

      if (matrix[row][col] !== word[k]) {
        return false;
      }
    }

    return true
  }

  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      if (matrix[i][j] !== word[0]) continue

      for (const [rowStep, colStep] of directions) {
        if (checkDirection(i, j, rowStep, colStep)) return true
      }
    }
  }

  return false;
}

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

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.