Count the ways words from a list can be traced in a letter grid along paths that step only right or down and turn at most once.
Unlock detailed company stats for this questionUpgrade
You are given a grid of letters and a list of distinct words. Count the total number of ways the words can be traced in the grid. A trace is a path of neighbouring cells that spells the word, under these rules: it can begin at any cell; every step moves one cell to the right or one cell down; and the path can turn no more than once, so it is a straight run, a run to the right followed by a run down, or a run down followed by a run to the right. Different paths for the same word count separately, and the counts for all the words are added together. Running time proportional to rows x columns x number of words x longest word length is acceptable. Example: in the two-row grid 'pin' over 'ink', the word 'pink' can be traced two ways (right, right, down and down, right, right); the path right, down, right also spells it but turns twice, so it does not count; 'nip' cannot be traced at all because it would need leftward steps. For the words ['pink', 'nip'] the answer is 2.
Community answers
No answers contributed by the community yet.
Related questions
Given a grid of size n x n, return all the paths from top-left corner to bottom-right corner. You can only move right or down.Related courses

Course

