Skip to main content
All Questions

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 courses

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.