FastPrepWord Path Moving Only Right or Down

Word Path Moving Only Right or Down

Atlassian logoAtlassian● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Find a path spelling word in a rectangular character grid. The word may start at any cell. Each next letter must be in an immediately adjacent cell to the right or directly below.

Return one coordinate [row,column] for every letter, in word order. For this exercise, assume zero-based coordinates and represent the grid by equal-length lowercase row strings. Both grid and word are nonempty.

For this exercise, assume the lexicographically smallest complete coordinate path must be returned. Compare at the first differing coordinate, row before column. Choose starting cells in row-major order and prefer right before down only when that continuation can complete the word.

For this exercise, assume the word may be absent; return an empty coordinate array when no permitted path spells it.

For this exercise, use the right/down movement shown by both literal source paths. The primary coordinate-output report leaves movement directions unstated, and another report's unfinished code attempt uses up instead of down.

Function

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

Examples

Example 1

grid = ["bbballoo","baccescn","altewcew","alossecc","woowacaw","ibwowwow"]word = "access"return = [[1,1],[1,2],[1,3],[2,3],[3,3],[3,4]]

The original source6683635 grid, word and coordinate path are retained.

Example 2

grid = ["bbballoo","baccescn","altewcew","alossecc","woowacaw","ibwowwow"]word = "balloon"return = [[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[1,7]]

The original source6683635 second word and path are retained.

Example 3

grid = ["ab","cd"]word = "aa"return = []

This authored example returns the declared empty result for absence.

Constraints

  • For this exercise, assume height and width are each from 1 through 12, and all row lengths are equal.
  • 1 <= word.length <= height + width - 1.
  • All characters are lowercase English letters.

More Atlassian problems

See Atlassian hiring insights
public int[][] gridPath(String[] grid, String word) {
    // Write your code here.
    return new int[0][0];
}
grid["bbballoo","baccescn","altewcew","alossecc","woowacaw","ibwowwow"]
word"access"
expected[[1,1],[1,2],[1,3],[2,3],[3,3],[3,4]]
Checking account…