Greedy Grid Word Occurrences with No Reused Cells
Problem statement
Find repeated occurrences of word in a rectangular character grid. Return their coordinates without using a cell in more than one selected occurrence.
The report describes a no-reuse follow-up but omits its precise scope, directions and selection policy. For this exercise, assume no-reuse is global across completed occurrences, and each path starts anywhere and moves only to an adjacent cell to the right or directly below.
For this exercise, assume the following deterministic procedure: among paths that spell the complete word using unused cells, select the lexicographically smallest coordinate sequence; mark its cells permanently used; and repeat until no path remains. Compare coordinates by row then column, using zero-based indices. A failed exploratory branch never consumes a cell.
This greedy procedure is the defined practice rule; it need not maximize the number of occurrences.
Return all selected coordinates in one int[][]. For this exercise, assume each consecutive block of word.length rows represents one path, and blocks appear in selection order. If no path exists, return an empty array.
Function
disjointWordPaths(grid: String[], word: String) → int[][]Examples
Example 1
grid = ["abab","abab"]word = "ab"return = [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3]]This authored example selects four consecutive two-cell paths and uses each cell once.
Example 2
grid = ["aa","aa"]word = "aa"return = [[0,0],[0,1],[1,0],[1,1]]This authored example selects the top horizontal path before the bottom horizontal path.
Example 3
grid = ["aaa","abb","aaa"]word = "aab"return = [[0,0],[0,1],[1,1]]This authored example selects [0,0],[0,1],[1,1] and stops. A different choice could produce two paths, but maximizing their number is not the stated practice rule.
Constraints
- For this exercise, assume grid height and width are each from
1through10, and row strings have equal length. 1 <= word.length <= height + width - 1.- All characters are lowercase English letters.
- No coordinate occurs twice in the result.