FastPrepFind a Word Path Moving Right or Down

Find a Word Path Moving Right or Down

Intuit logoIntuit● MediumNEW GRADINTERNOA

Problem statement

A target word is hidden in a nonempty rectangular letter grid. The word may start at any cell. Every next letter must be in the cell directly below or directly to the right of the previous letter. Turns are allowed; upward, leftward, and diagonal moves are not.

The input guarantees that the target has exactly zero or one complete matching coordinate path. Return every coordinate in that path as [row,column] pairs in word order, using zero-based indices. For this exercise, assume an empty array represents the source's no-match result None.

For this exercise, assume each grid row is supplied as a complete string of ASCII letters, the target is a nonempty ASCII-letter string, and matching is case-sensitive. Return the path, rather than a boolean or the locations of all cells containing any target letter.

Function

findWord(word: String, grid: String[]) → int[][]

Examples

Example 1

word = "catnip"grid = ["crcars","abitnb","tfnnti","xsiipt"]return = [[0,2],[0,3],[1,3],[2,3],[3,3],[3,4]]

The cells spell c, a, t, n, i, p. From (0,2), move right to (0,3), then down three times to (3,3), then right to (3,4). These are all six coordinates in the unique complete path.

Example 2

word = "carsick"grid = ["crcars","abitnb","tfnnti","xscatn","xsddea","sqwxsp"]return = []

The original six-by-six public test has no matching path. In particular, there is no k anywhere in this grid, so the complete target carsick cannot appear. Return an empty array for None.

Constraints

  • For this exercise, assume 1 <= grid.length <= 30, with all rows having the same length from 1 to 30.
  • For this exercise, assume 1 <= word.length <= 60.
  • Every grid and target character is an ASCII letter A through Z or a through z. There is at most one complete right/down matching path for the supplied target.

More Intuit problems

See Intuit hiring insights
Saved
public int[][] findWord(String word, String[] grid) {
    // Write your code here
}
word"catnip"
grid["crcars","abitnb","tfnnti","xsiipt"]
expected[[0,2],[0,3],[1,3],[2,3],[3,3],[3,4]]
Checking account…