Count Right-or-Down Word Paths with One Turn
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[]) → intExamples
Example 1
board = ["abc","def","ghi"]words = ["abc","adg","aei"]return = 2Horizontal, vertical, and one-turn paths are all eligible.
Example 2
board = ["aa","ab"]words = ["a"]return = 3Each matching cell contributes once.
Constraints
1 <= board rows, columns <= 501 <= words.length <= 1001 <= words[i].length <= 100