Solución
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