1
- // n: length of words, k: length of word, h: height of board, w: width of board
2
- // Time complexity: O(4^k * h * w * n )
1
+ // k: length of word, h: height of board, w: width of board
2
+ // Time complexity: O(4^k * h * w)
3
3
// Space complexity: O(4^k)
4
4
5
+ class Node {
6
+ constructor ( value = "" ) {
7
+ this . value = value ;
8
+ this . children = new Map ( ) ;
9
+ this . isEnd = false ;
10
+ }
11
+ }
12
+ class Trie {
13
+ constructor ( ) {
14
+ this . head = new Node ( ) ;
15
+ }
16
+
17
+ add ( str ) {
18
+ let current = this . head ;
19
+
20
+ for ( const chr of str ) {
21
+ if ( ! current . children . has ( chr ) ) {
22
+ current . children . set ( chr , new Node ( current . value + chr ) ) ;
23
+ }
24
+
25
+ current = current . children . get ( chr ) ;
26
+ }
27
+
28
+ current . isEnd = true ;
29
+ }
30
+ }
31
+
5
32
/**
6
33
* @param {character[][] } board
7
34
* @param {string[] } words
8
35
* @return {string[] }
9
36
*/
10
37
var findWords = function ( board , words ) {
38
+ const answer = new Set ( ) ;
39
+
11
40
const h = board . length ;
12
41
const w = board [ 0 ] . length ;
13
42
@@ -17,63 +46,42 @@ var findWords = function (board, words) {
17
46
Array . from ( { length : w } , ( ) => false )
18
47
) ;
19
48
20
- const findWord = ( current , i , word ) => {
49
+ const dfs = ( current , children ) => {
21
50
const [ cy , cx ] = current ;
22
51
23
- if ( i === word . length - 1 ) {
24
- return true ;
52
+ if ( ! children . has ( board [ cy ] [ cx ] ) ) {
53
+ return ;
25
54
}
26
55
27
- let found = false ;
56
+ if ( children . get ( board [ cy ] [ cx ] ) . isEnd ) {
57
+ answer . add ( children . get ( board [ cy ] [ cx ] ) . value ) ;
58
+ }
28
59
29
60
for ( let j = 0 ; j < dx . length ; j ++ ) {
30
61
const nx = cx + dx [ j ] ;
31
62
const ny = cy + dy [ j ] ;
32
63
33
64
if ( nx >= 0 && nx < w && ny >= 0 && ny < h && ! checked [ ny ] [ nx ] ) {
34
- if ( board [ ny ] [ nx ] === word [ i + 1 ] ) {
35
- checked [ ny ] [ nx ] = true ;
36
-
37
- if ( findWord ( [ ny , nx ] , i + 1 , word ) ) {
38
- found = true ;
39
- }
40
-
41
- checked [ ny ] [ nx ] = false ;
42
- }
65
+ checked [ ny ] [ nx ] = true ;
66
+ dfs ( [ ny , nx ] , children . get ( board [ cy ] [ cx ] ) . children ) ;
67
+ checked [ ny ] [ nx ] = false ;
43
68
}
44
69
}
45
-
46
- return found ;
47
70
} ;
48
71
49
- const answer = [ ] ;
72
+ const trie = new Trie ( ) ;
50
73
51
74
for ( const word of words ) {
52
- let found = false ;
53
-
54
- for ( let i = 0 ; i < h ; i ++ ) {
55
- if ( found ) {
56
- break ;
57
- }
58
-
59
- for ( let j = 0 ; j < w ; j ++ ) {
60
- if ( found ) {
61
- break ;
62
- }
63
-
64
- if ( board [ i ] [ j ] === word [ 0 ] ) {
65
- checked [ i ] [ j ] = true ;
66
-
67
- if ( findWord ( [ i , j ] , 0 , word ) ) {
68
- answer . push ( word ) ;
69
- found = true ;
70
- }
75
+ trie . add ( word ) ;
76
+ }
71
77
72
- checked [ i ] [ j ] = false ;
73
- }
74
- }
78
+ for ( let i = 0 ; i < h ; i ++ ) {
79
+ for ( let j = 0 ; j < w ; j ++ ) {
80
+ checked [ i ] [ j ] = true ;
81
+ dfs ( [ i , j ] , trie . head . children ) ;
82
+ checked [ i ] [ j ] = false ;
75
83
}
76
84
}
77
85
78
- return answer ;
86
+ return [ ... answer ] ;
79
87
} ;
0 commit comments