| 16 | 18 | * @returns {{start: number[], end: number[]}|undefined} |
| 17 | 19 | */ |
| 18 | 20 | find(word) { |
| 19 | | throw new Error('Remove this line and implement the function'); |
| 21 | // Edge Case: Empty word |
| 22 | if (!word || word.length === 0) { |
| 23 | return undefined; |
| 24 | } |
| 25 | |
| 26 | // Edge Case: Empty grid |
| 27 | if (this.rows === 0 || this.cols === 0) { |
| 28 | return undefined; |
| 29 | } |
| 30 | |
| 31 | const searchWord = word.toUpperCase(); |
| 32 | |
| 33 | // Edge Case: Word longer than grid dimensions |
| 34 | if (searchWord.length > this.rows && searchWord.length > this.cols) { |
| 35 | return undefined; |
| 36 | } |
| 37 | |
| 38 | // Define all 8 directions: right, left, down, up, down-right, down-left, up-right, up-left |
| 39 | const directions = [ |
| 40 | [0, 1], // right |
| 41 | [0, -1], // left |
| 42 | [1, 0], // down |
| 43 | [-1, 0], // up |
| 44 | [1, 1], // down-right |
| 45 | [1, -1], // down-left |
| 46 | [-1, 1], // up-right |
| 47 | [-1, -1] // up-left |
| 48 | ]; |
| 49 | |
| 50 | // Try each cell as a starting point |
| 51 | for (let row = 0; row < this.rows; row++) { |
| 52 | for (let col = 0; col < this.cols; col++) { |
| 53 | // Try each direction from this cell |
| 54 | for (const [dx, dy] of directions) { |
| 55 | const result = this.checkWordAtPosition(searchWord, row, col, dx, dy); |
| 56 | if (result) { |
| 57 | return result; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Edge Case: Word not found in grid |
| 64 | return undefined; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check if word exists at given position going in given direction |
| 69 | * @param {string} word |
| 70 | * @param {number} startRow |
| 71 | * @param {number} startCol |
| 72 | * @param {number} dx |
| 73 | * @param {number} dy |
| 74 | * @returns {{start: number[], end: number[]}|undefined} |
| 75 | */ |
| 76 | checkWordAtPosition(word, startRow, startCol, dx, dy) { |
| 77 | // Edge Case: First letter doesn't match |
| 78 | if (this.grid[startRow][startCol] !== word[0]) { |
| 79 | return undefined; |
| 80 | } |
| 81 | |
| 82 | const endRow = startRow + dx * (word.length - 1); |
| 83 | const endCol = startCol + dy * (word.length - 1); |
| 84 | |
| 85 | // Edge Case: Word goes out of bounds |
| 86 | if (endRow < 0 || endRow >= this.rows || endCol < 0 || endCol >= this.cols) { |
| 87 | return undefined; |
| 88 | } |
| 89 | |
| 90 | // Check each letter of the word |
| 91 | for (let i = 0; i < word.length; i++) { |
| 92 | const row = startRow + dx * i; |
| 93 | const col = startCol + dy * i; |
| 94 | |
| 95 | // Edge Case: Letter mismatch at position |
| 96 | if (this.grid[row][col] !== word[i]) { |
| 97 | return undefined; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Convert to 1-indexed coordinates as per expected output format |
| 102 | return { |
| 103 | start: [startCol + 1, startRow + 1], |
| 104 | end: [endCol + 1, endRow + 1] |
| 105 | }; |
| 20 | 106 | } |
| 21 | 107 | } |
| 22 | 108 | |
| 23 | 109 | export default WordSearch; |
| 110 | |
| 111 | // Handled Edge Cases: Empty word, Empty grid, Word longer than grid dimensions, Word not found in grid, First letter doesn't match, Word goes out of bounds, Letter mismatch at position |