Accenture · Question 62 · Backtracking
Word Search II
Problem
Given a 2D board and a list of words, find all words in the board.
Approach
Use Trie and DFS for efficient search.
Time O(M * N * 4^L)Space O(K * L)
How it works
- 1Index all words in a Trie.
- 2DFS follows Trie edges only; driver sorts for stability.
Sample input / output
Input: board = 4x4 (oath grid), words = ["oath","pea","eat","rain"] Output: [eat, oath]
Constraints
1 <= m, n <= 12; lowercase board and words.
Solution — read it, then walk it
Time O(M * N * 4^L)Space O(K * L)Loops 3Branches 5Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/43
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits