Solución
solution.tsTypeScript
export function wordSearch(grid: string[][], word: string): boolean {
// Escribe tu solución aquí
let found: boolean = false;
for (let rowIndex in grid) {
for (let columnIndex in grid[rowIndex]) {
let value = grid[rowIndex][columnIndex];
if (value === word[0]) {
found = charSearch(+rowIndex, +columnIndex, grid, 0, word);
if(found) return true;
}
}
}
return found;
}
function charSearch(rowIndex: number, columnIndex: number, grid: string[][], letterIndex: number, word: string): boolean {
if (rowIndex < 0 || rowIndex >= grid.length) return false;
if (columnIndex < 0 || columnIndex >= grid[rowIndex].length) return false;
if(word[letterIndex] !== grid[rowIndex][columnIndex]) return false;
if(letterIndex === word.length - 1) return true;
let temp = grid[rowIndex][columnIndex];
grid[rowIndex][columnIndex] = '*';
let down = charSearch(+rowIndex + 1, +columnIndex, grid, letterIndex + 1, word);
let up = charSearch(+rowIndex - 1, +columnIndex, grid, letterIndex + 1, word);
let right = charSearch(+rowIndex, +columnIndex + 1, grid, letterIndex + 1, word);
let left = charSearch(+rowIndex, +columnIndex - 1, grid, letterIndex + 1, word);
grid[rowIndex][columnIndex] = temp;
return right || left || up || down;
}0respuestas