FastPrepCount Right-or-Down Word Paths with One Turn

Count Right-or-Down Word Paths with One Turn

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

For each word, count distinct coordinate paths in board that spell it. A path starts anywhere, moves one cell right or down per character, and changes direction at most once. A path never leaves the board.

Sum occurrences over all words, counting duplicate words independently. A one-character occurrence is counted once per matching cell.

Function

countWordPaths(board: String[], words: String[]) → int

Examples

Example 1

board = ["abc","def","ghi"]words = ["abc","adg","aei"]return = 2

Horizontal, vertical, and one-turn paths are all eligible.

Example 2

board = ["aa","ab"]words = ["a"]return = 3

Each matching cell contributes once.

Constraints

  • 1 <= board rows, columns <= 50
  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int countWordPaths(String[] board, String[] words) {
    // Write your code here.
}
board["abc","def","ghi"]
words["abc","adg","aei"]
expected2
Checking account…