forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0079-word-search.c
54 lines (50 loc) · 1.36 KB
/
0079-word-search.c
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
/*
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
Time: O(n*m*(3^h)) where h is the length of the word
Space: O(1)
*/
bool dfs(char** board, int n, int m, int i, int j, char* word) {
if (word[0]=='\0' || word[1]=='\0')
return true;
char c = board[i][j];
board[i][j] = 0;
if (i>0 && board[i-1][j]==word[1]){
if (dfs(board, n, m, i-1, j, word+1)){
board[i][j] = c;
return true;
}
}
if (j>0 && board[i][j-1]==word[1]){
if (dfs(board, n, m, i, j-1, word+1)){
board[i][j] = c;
return true;
}
}
if (i<(n-1) && board[i+1][j]==word[1]){
if (dfs(board, n, m, i+1, j, word+1)){
board[i][j] = c;
return true;
}
}
if (j<(m-1) && board[i][j+1]==word[1]){
if (dfs(board, n, m, i, j+1, word+1)){
board[i][j] = c;
return true;
}
}
board[i][j] = c;
return false;
}
bool exist(char** board, int boardSize, int* boardColSize, char * word){
int m = (*boardColSize), i, j;
for (i=0; i<boardSize; i++) {
for (j=0; j<m; j++) {
if (board[i][j]==word[0]) {
if (dfs(board, boardSize, m, i, j, word)){
return true;
}
}
}
}
return false;
}