My first instinct was to reach for the classic backtracking word search approach and I started explaining it before realizing they said no turning.
Treat each cell as a potential starting point and each of the 8 directions as a ray to follow. For each start-direction pair, walk step by step while the characters match the target word, returning true if the entire word is consumed. This brute-force scan is O(R*C*8*L) time and O(1) extra space, which is optimal for this problem.
Pro tip: Before coding, explicitly state that you'll handle edge cases like empty word, word longer than grid dimensions, and single-character words. Also mention that you can prune directions early if the remaining grid length in that direction is shorter than the word.
Confirm the matrix dimensions, character set, and whether the word can be empty. Check if the word length exceeds the maximum possible line length in any direction; if so, return false immediately.
Represent the 8 directions as pairs of row and column deltas: (-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1).
For each cell (r, c) in the grid, if grid[r][c] matches the first character of the word, try each of the 8 directions from that cell.
Starting from the cell, move step by step in the direction, checking that each character matches the corresponding character in the word. Stop if you go out of bounds or find a mismatch. If you match all characters, return true.
If no start-direction pair matches the entire word, return false. State the time complexity O(R*C*8*L) and space complexity O(1).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.