forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
46 lines (42 loc) · 1.58 KB
/
Solution.cs
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
namespace LeetCodeNet.G0001_0100.S0079_word_search {
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n)
// #2024_01_05_Time_152_ms_(99.69%)_Space_42.3_MB_(26.96%)
public class Solution {
public bool Exist(char[][] board, string word) {
for (int i = 0; i < board.Length; i++) {
for (int j = 0; j < board[0].Length; j++) {
char ch = word[0];
if (board[i][j] == ch) {
if (helper(i, j, board, word, 1)) {
return true;
}
}
}
}
return false;
}
private bool helper(int r, int c, char[][] board, string word, int count) {
if (count == word.Length) {
return true;
}
char currChar = board[r][c];
board[r][c] = '!';
char nextChar = word[count];
if (r > 0 && board[r - 1][c] == nextChar) {
if (helper(r - 1, c, board, word, count + 1)) return true;
}
if (r < board.Length - 1 && board[r + 1][c] == nextChar) {
if (helper(r + 1, c, board, word, count + 1)) return true;
}
if (c > 0 && board[r][c - 1] == nextChar) {
if (helper(r, c - 1, board, word, count + 1)) return true;
}
if (c < board[0].Length - 1 && board[r][c + 1] == nextChar) {
if (helper(r, c + 1, board, word, count + 1)) return true;
}
board[r][c] = currChar;
return false;
}
}
}