| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {string[]} input |
| 8 | | * @returns {string[]} |
| 7 | * Annotates a flower field grid by replacing empty spaces with the count of adjacent flowers. |
| 8 | * @param {string[]} input - An array of strings representing the flower field grid. |
| 9 | * @returns {string[]} - An array of strings with empty spaces replaced by flower counts or left empty. |
| 10 | * |
| 11 | * Edge Cases Handled: |
| 12 | * - Empty input array (returns empty array) |
| 13 | * - Input array with empty strings (treats as valid grid with no cells) |
| 14 | * - Grid with no empty spaces (returns original grid) |
| 15 | * - Grid with no flowers (leaves all empty spaces as they are) |
| 16 | * - Adjacent flower counting at grid boundaries (top, bottom, left, right edges and corners) |
| 9 | 17 | */ |
| 10 | 18 | export const annotate = (input) => { |
| 11 | | throw new Error('Remove this statement and implement this function'); |
| 19 | // Edge Case: Empty input array |
| 20 | if (!input || input.length === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | const rows = input.length; |
| 25 | const cols = input[0] ? input[0].length : 0; |
| 26 | |
| 27 | // Edge Case: Input with empty strings |
| 28 | if (cols === 0) { |
| 29 | return input; |
| 30 | } |
| 31 | |
| 32 | // Create a result grid as an array of arrays for easier manipulation |
| 33 | const result = Array(rows); |
| 34 | for (let i = 0; i < rows; i++) { |
| 35 | result[i] = new Array(cols); |
| 36 | } |
| 37 | |
| 38 | // Define the 8 possible directions for adjacent cells |
| 39 | const directions = [ |
| 40 | [-1, -1], [-1, 0], [-1, 1], |
| 41 | [0, -1], [0, 1], |
| 42 | [1, -1], [1, 0], [1, 1] |
| 43 | ]; |
| 44 | |
| 45 | // Iterate through each cell in the grid |
| 46 | for (let row = 0; row < rows; row++) { |
| 47 | for (let col = 0; col < cols; col++) { |
| 48 | // If the cell is empty, count adjacent flowers |
| 49 | if (input[row][col] === ' ') { |
| 50 | let flowerCount = 0; |
| 51 | |
| 52 | // Check all 8 adjacent cells |
| 53 | for (const [dx, dy] of directions) { |
| 54 | const newRow = row + dx; |
| 55 | const newCol = col + dy; |
| 56 | |
| 57 | // Edge Case: Boundary checking to prevent out-of-bounds access |
| 58 | if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) { |
| 59 | if (input[newRow][newCol] === '*') { |
| 60 | flowerCount++; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // If there are adjacent flowers, replace the empty space with the count |
| 66 | // Otherwise, leave it as an empty space |
| 67 | if (flowerCount > 0) { |
| 68 | result[row][col] = flowerCount.toString(); |
| 69 | } else { |
| 70 | result[row][col] = ' '; |
| 71 | } |
| 72 | } else { |
| 73 | // If the cell is a flower, keep it as is |
| 74 | result[row][col] = input[row][col]; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Convert the result grid back to an array of strings |
| 80 | return result.map(row => row.join('')); |
| 12 | 81 | }; |
| 82 | |
| 83 | // Handled Edge Cases: Empty input array, Input with empty strings, Grid with no empty spaces, Grid with no flowers, Adjacent flower counting at grid boundaries |