91. Word Search II
Find all supplied words that can be spelled by moving through orthogonally adjacent board cells. A path for one word cannot use the same coordinate twice.
Different words may reuse cells in their separate searches. Leave the board unchanged and follow the contract's uniqueness and output-order requirements.
Example 1
Input
board=[oa,ht]; words=[o,oa,oath,oath,hat,ao,oao]
Output
[ao,o,oa,oath]
Explanation: The path o→a→t→h finds oath; oa stays searchable after o is emitted. Duplicate input/path occurrences emit one word, and oao would reuse the first o.
Example 2
Input
board=[aa]; words=[a,aa,aaa,aa]
Output
[a,aa]
Explanation: Two distinct a coordinates can spell aa, but no third coordinate exists.
Example 3
Input
board=[ab,cd]; words=[ad,ac,bd]
Output
[ac,bd]
Explanation: ad would require a diagonal edge and is rejected.
Constraints
0 <= rows, columns <= 120 <= words.length <= 1041 <= words[i].length <= 144sum(words[i].length) <= 105- Board cells and dictionary words contain lowercase English letters. Words are nonempty; no cell can be reused within one word path.
Hints
Hint 1
Several words can share one prefix, so consider a trie.
Hint 2
State must track both the dictionary prefix and the current no-reuse board path.
Hint 3
Mark on entry, consume a terminal only once, continue its descendants, and unmark on backtrack.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Shared prefixes, duplicate words, and local terminal consumption
["oa","ht"]
["o","oa","oath","oath","hat","ao","oao"]
["ao","o","oa","oath"]