ExamAdda Logo

Word Search II

Easy

Given an m x n board of characters and a list of strings words, return all words from words that can be found on the board.

A word can be constructed from letters of sequentially adjacent cells, where adjacent cells are connected horizontally or vertically.

The same cell cannot be used more than once while forming a single word.

Return the found words in any order.

Example 1

Input

4 4
o a a n
e t a e
i h k r
i f l v
4
oath
pea
eat
rain

Output

oath eat

Explanation

oath and eat can be formed using adjacent cells.

pea and rain cannot be formed.

Example 2

Input

2 2
a b
c d
2
ab
ac

Output

ab ac

Explanation

  • ab can be formed from a → b.
  • ac can be formed from a → c.

Both words are found.

Constraints

1 <= m
n <= 12
1 <= m * n <= 144
1 <= words.length <= 3 × 10⁴
1 <= words[i].length <= 10
board[i][j] consists of lowercase English letters.
words[i] consists of lowercase English letters.
All words are distinct.

Hints:

Hint 1

Build a Trie containing all the words.

Hint 2

Start DFS from every cell on the board. Use the Trie to determine whether the current path can form a valid word.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 20, 2026

Last updated

Aug 20, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output