Trie
Array
String
Backtracking
Matrix
Description
Given an m x n
board
of characters and a list of strings words
, return all words on the board .
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
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 2:
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
Constraints:
m == board.length
n == board[i].length
1 <= m, n <= 12
board[i][j]
is a lowercase English letter.
1 <= words.length <= 3 * 104
1 <= words[i].length <= 10
words[i]
consists of lowercase English letters.
All the strings of words
are unique.
Solutions
Solution 1
Python3 Java C++ Go TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 class Trie :
def __init__ ( self ):
self . children : List [ Trie | None ] = [ None ] * 26
self . ref : int = - 1
def insert ( self , w : str , ref : int ):
node = self
for c in w :
idx = ord ( c ) - ord ( 'a' )
if node . children [ idx ] is None :
node . children [ idx ] = Trie ()
node = node . children [ idx ]
node . ref = ref
class Solution :
def findWords ( self , board : List [ List [ str ]], words : List [ str ]) -> List [ str ]:
def dfs ( node : Trie , i : int , j : int ):
idx = ord ( board [ i ][ j ]) - ord ( 'a' )
if node . children [ idx ] is None :
return
node = node . children [ idx ]
if node . ref >= 0 :
ans . append ( words [ node . ref ])
node . ref = - 1
c = board [ i ][ j ]
board [ i ][ j ] = '#'
for a , b in pairwise (( - 1 , 0 , 1 , 0 , - 1 )):
x , y = i + a , j + b
if 0 <= x < m and 0 <= y < n and board [ x ][ y ] != '#' :
dfs ( node , x , y )
board [ i ][ j ] = c
tree = Trie ()
for i , w in enumerate ( words ):
tree . insert ( w , i )
m , n = len ( board ), len ( board [ 0 ])
ans = []
for i in range ( m ):
for j in range ( n ):
dfs ( tree , i , j )
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 class Trie {
Trie [] children = new Trie [ 26 ] ;
int ref = - 1 ;
public void insert ( String w , int ref ) {
Trie node = this ;
for ( int i = 0 ; i < w . length (); ++ i ) {
int j = w . charAt ( i ) - 'a' ;
if ( node . children [ j ] == null ) {
node . children [ j ] = new Trie ();
}
node = node . children [ j ] ;
}
node . ref = ref ;
}
}
class Solution {
private char [][] board ;
private String [] words ;
private List < String > ans = new ArrayList <> ();
public List < String > findWords ( char [][] board , String [] words ) {
this . board = board ;
this . words = words ;
Trie tree = new Trie ();
for ( int i = 0 ; i < words . length ; ++ i ) {
tree . insert ( words [ i ] , i );
}
int m = board . length , n = board [ 0 ] . length ;
for ( int i = 0 ; i < m ; ++ i ) {
for ( int j = 0 ; j < n ; ++ j ) {
dfs ( tree , i , j );
}
}
return ans ;
}
private void dfs ( Trie node , int i