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)
33// Space complexity: O(4^k)
44
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+
532/**
633 * @param {character[][] } board
734 * @param {string[] } words
835 * @return {string[] }
936 */
1037var findWords = function ( board , words ) {
38+ const answer = new Set ( ) ;
39+
1140 const h = board . length ;
1241 const w = board [ 0 ] . length ;
1342
@@ -17,63 +46,42 @@ var findWords = function (board, words) {
1746 Array . from ( { length : w } , ( ) => false )
1847 ) ;
1948
20- const findWord = ( current , i , word ) => {
49+ const dfs = ( current , children ) => {
2150 const [ cy , cx ] = current ;
2251
23- if ( i === word . length - 1 ) {
24- return true ;
52+ if ( ! children . has ( board [ cy ] [ cx ] ) ) {
53+ return ;
2554 }
2655
27- let found = false ;
56+ if ( children . get ( board [ cy ] [ cx ] ) . isEnd ) {
57+ answer . add ( children . get ( board [ cy ] [ cx ] ) . value ) ;
58+ }
2859
2960 for ( let j = 0 ; j < dx . length ; j ++ ) {
3061 const nx = cx + dx [ j ] ;
3162 const ny = cy + dy [ j ] ;
3263
3364 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 ;
4368 }
4469 }
45-
46- return found ;
4770 } ;
4871
49- const answer = [ ] ;
72+ const trie = new Trie ( ) ;
5073
5174 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+ }
7177
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 ;
7583 }
7684 }
7785
78- return answer ;
86+ return [ ... answer ] ;
7987} ;
0 commit comments