← 返回 bytedance 的题目列表Word Search II (find all words on board)
类型:online_judge
Problem: Find All Words in a Grid (Word Search II)
Given an m x n character grid board and an array of strings words, return all words from words that can be found in the grid.
Matching rules
A word is constructed from letters of sequentially adjacent cells.
Adjacent means up, down, left, right (no diagonals).
The same cell may not be used more than once in a single word.
Output
Return all found words (each word at most once). Order does not matter.
Constraints (common setting)
1 <= m, n <= 12
1 <= words.length <= 3 * 10^4
1 <= words[i].length <= 10
board[i][j] are lowercase letters
words[i] are lowercase letters
Example
Input:
board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]]
words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Example
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
eat oath