word-search

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

1
2
3
4
5
6
7
8
9
10
11
12
Example:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.

Notes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DFS would be te desired approach.

We can iterate the arrays and when we find the first matching letter we start testing the neighbors
and continue to do this until we find all of them...if it fails somewhere we backtrack and try next one

For 'SEE'

We find 'S' at board[2][0]

need functions for getting each neighbor

check top neighbor, it's 'A'
check left neigbor, none
check right neighbor, 'F'
check bottom neighbor, 'A'

not found so keep iterating

note we need to ensure that we mark visited nodes
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
bool dfs(vector<vector<char>>& board, string word, int i,  int j, int p) {
if ((i < 0) || (j < 0) || (i >= board.size()) || (j >= board[0].size())) return false;

if (p >= word.size()) return true;

if (word[p] == board[i][j]) {
char c = board[i][j];// for caching currently inspected char
//cout << "checking " << c << endl;
board[i][j] = 0;//erase temporarily to mark as visited
// check top neighbor
//cout << "check top\n";
if (dfs(board, word, i - 1, j, p+1)) return true;
// bottom
//cout << "check bottom\n";
if (dfs(board, word, i + 1, j, p+1)) return true;
// left
//cout << "check left\n";
if (dfs(board, word, i, j - 1, p+1)) return true;
//right
//cout << "check right\n";
if (dfs(board, word, i , j + 1 , p+1)) return true;

// we need to restore char at end of each run
board[i][j] = c;
}

return false;
}

bool exist(vector<vector<char>>& board, string word) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
if (dfs(board, word, i, j, 0)) return true;
}
}
return false;
}

This works for most cases but here is an example where it fails 😧

1
2
board = [["a"]]
word = "a"

We need to consider the case of single letter words.

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
bool dfs(vector<vector<char>>& board, string word, int i,  int j, int p) {
if ((i < 0) || (j < 0) || (i >= board.size()) || (j >= board[0].size())) return false;

if (p >= word.size()) return true;

if (word[p] == board[i][j]) {
char c = board[i][j];// for caching currently inspected char
//cout << "checking " << c << endl;
board[i][j] = 0;//erase temporarily to mark as visited
// check top neighbor
//cout << "check top\n";
if (dfs(board, word, i - 1, j, p+1)) return true;
// bottom
//cout << "check bottom\n";
if (dfs(board, word, i + 1, j, p+1)) return true;
// left
//cout << "check left\n";
if (dfs(board, word, i, j - 1, p+1)) return true;
//right
//cout << "check right\n";
if (dfs(board, word, i , j + 1 , p+1)) return true;

if (word.size() == 1) return true;

// we need to restore char at end of each run
board[i][j] = c;
}

return false;
}

bool exist(vector<vector<char>>& board, string word) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
if (dfs(board, word, i, j, 0)) return true;
}
}
return false;
}