@@ -8,21 +8,52 @@ public class _0079_WordSearch_3 {
88 * @since 2025-11-15 08:52:34
99 */
1010 public boolean exist (char [][] board , String word ) {
11+ // 优化一:检查字母出现次数
12+ char [] counter = new char [52 ];
13+ for (char [] chars : board ) {
14+ for (char c : chars ) {
15+ counter [getIndex (c )]++;
16+ }
17+ }
18+ char [] cnt = new char [52 ];
19+ for (char c : word .toCharArray ()) {
20+ cnt [getIndex (c )]++;
21+ }
22+ for (int i = 0 ; i < cnt .length ; i ++) {
23+ if (cnt [i ] > counter [i ]) {
24+ return false ;
25+ }
26+ }
27+
28+ // 优化二:如果最后字符出现次数更小,则从它开始,回溯次数更少
29+ int ti = getIndex (word .charAt (word .length () - 1 ));
30+ int hi = getIndex (word .charAt (0 ));
31+ if (cnt [ti ] < cnt [hi ]) {
32+ word = new StringBuilder (word ).reverse ().toString ();
33+ }
34+
35+ // 回溯查找
1136 for (int c = 0 ; c < board .length ; c ++) {
1237 for (int r = 0 ; r < board [c ].length ; r ++) {
13- if (board [c ][r ] == word .charAt (0 )) {
14- boolean found = backtrack (board , word , c , r , 0 );
15- if (found ) {
16- return true ;
17- }
38+ boolean found = backtrack (board , c , r , word , 0 );
39+ if (found ) {
40+ return true ;
1841 }
1942 }
2043 }
2144 return false ;
2245 }
2346
24- private boolean backtrack (char [][] board , String word ,
25- int column , int row , int index ) {
47+ private int getIndex (char c ) {
48+ int index = c - 'A' ;
49+ if (c >= 'a' ) {
50+ index = (c - 'a' ) + 26 ;
51+ }
52+ return index ;
53+ }
54+
55+ private boolean backtrack (char [][] board , int column , int row ,
56+ String word , int index ) {
2657 if (index == word .length ()) {
2758 return true ;
2859 }
@@ -33,22 +64,22 @@ private boolean backtrack(char[][] board, String word,
3364 }
3465 board [column ][row ] = '.' ;
3566 // 上
36- boolean found = backtrack (board , word , column - 1 , row , index + 1 );
67+ boolean found = backtrack (board , column - 1 , row , word , index + 1 );
3768 if (found ) {
3869 return true ;
3970 }
4071 // 下
41- found = backtrack (board , word , column + 1 , row , index + 1 );
72+ found = backtrack (board , column + 1 , row , word , index + 1 );
4273 if (found ) {
4374 return true ;
4475 }
4576 // 左
46- found = backtrack (board , word , column , row - 1 , index + 1 );
77+ found = backtrack (board , column , row - 1 , word , index + 1 );
4778 if (found ) {
4879 return true ;
4980 }
5081 // 右
51- found = backtrack (board , word , column , row + 1 , index + 1 );
82+ found = backtrack (board , column , row + 1 , word , index + 1 );
5283 if (found ) {
5384 return true ;
5485 }
0 commit comments