Skip to content

Commit

Permalink
word-search
Browse files Browse the repository at this point in the history
  • Loading branch information
taewanseoul committed Jan 3, 2025
1 parent 1793dd6 commit 121a34c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions word-search/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 268. Missing Number
* Given an m x n grid of characters board and a string word, return true if word exists in the grid.
* The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
*
* https://leetcode.com/problems/word-search/description/
*/

// O(n) time
// O(n) space
function exist(board: string[][], word: string): boolean {
let result = false;
const num_rows = board.length;
const num_cols = board[0].length;

function checkNeighbors(
board: (string | null)[][],
word: string,
row: number,
col: number,
startIndex: number
) {
const num_rows = board.length;
const num_cols = board[0].length;

if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) return;

if (board[row][col] !== word[startIndex]) return;

if (startIndex === word.length - 1) {
result = true;
return;
}

board[row][col] = null;
checkNeighbors(board, word, row + 1, col, startIndex + 1);
checkNeighbors(board, word, row - 1, col, startIndex + 1);
checkNeighbors(board, word, row, col + 1, startIndex + 1);
checkNeighbors(board, word, row, col - 1, startIndex + 1);
board[row][col] = word[startIndex];
}

for (let i = 0; i < num_rows; i++) {
for (let j = 0; j < num_cols; j++) {
if (board[i][j] === word[0]) {
checkNeighbors(board, word, i, j, 0);
if (result) return result;
}
}
}

return result;
}

0 comments on commit 121a34c

Please sign in to comment.