/
SearchAWordIn2DMatrix.java
102 lines (95 loc) · 3.51 KB
/
SearchAWordIn2DMatrix.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.redquark.techinterview.dsa.string;
/**
* @author Anirudh Sharma
* <p>
* Given an m x n grid of characters board and a string word, return true if word exists in the grid.
* <p>
* The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are
* horizontally or vertically neighboring. The same letter cell may not be used more than once.
* <p>
* Constraints:
* <p>
* m == board.length
* n = board[i].length
* 1 <= m, n <= 6
* 1 <= word.length <= 15
* board and word consists of only lowercase and uppercase English letters.
*/
public class SearchAWordIn2DMatrix {
private static boolean search(char[][] board, String word) {
// Special cases
if (board == null || board.length == 0) {
return false;
}
// Loop through every cell in the board
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
// Check if the first letter and the remaining
// letters in the words match
if (board[i][j] == word.charAt(0) && dfs(board, i, j, 0, word)) {
return true;
}
}
}
return false;
}
private static boolean dfs(char[][] board, int i, int j, int currentIndex, String word) {
// If the currentIndex is equal to the length of the
// word, it means we have found the word
if (currentIndex == word.length()) {
return true;
}
// If we are out of boundaries of the board or the
// characters don't match
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(currentIndex)) {
return false;
}
// Get the current character from the board
char temp = board[i][j];
// Make the current index in board as space
// to mark it as visited
board[i][j] = ' ';
// Check for all the words adjacent to the current
// character in the board
boolean doesExist = dfs(board, i, j + 1, currentIndex + 1, word)
|| dfs(board, i, j - 1, currentIndex + 1, word)
|| dfs(board, i + 1, j, currentIndex + 1, word)
|| dfs(board, i - 1, j, currentIndex + 1, word);
// Restore the value of current cell
board[i][j] = temp;
return doesExist;
}
public static void main(String[] args) {
char[][] board = new char[][]{
{'B', 'B', 'M', 'B', 'B', 'B'},
{'C', 'B', 'A', 'B', 'B', 'B'},
{'I', 'B', 'G', 'B', 'B', 'B'},
{'G', 'B', 'I', 'B', 'B', 'B'},
{'A', 'B', 'C', 'B', 'B', 'B'},
{'M', 'C', 'I', 'G', 'A', 'M'}
};
String word = "MAGIC";
System.out.println(search(board, word));
board = new char[][]{
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
word = "ABCCED";
System.out.println(search(board, word));
board = new char[][]{
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
word = "SEE";
System.out.println(search(board, word));
board = new char[][]{
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
word = "ABCB";
System.out.println(search(board, word));
}
}