-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWordPuzzle.java
131 lines (109 loc) · 3.62 KB
/
WordPuzzle.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* daily coding problem #98 */
/* Given a 2D board of characters 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.
*
* For example, given the following board:
*
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
* exists(board, "ABCCED") returns true,
* exists(board, "SEE") returns true, exists(board, "ABCB") returns false.
*/
public class WordPuzzle
{
public static void main(String[] args)
{
char[][] board = new char[][]{{'A','B','C','E','O'},{'S','F','C','S','P'},{'A','D','E','E','S'}};
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[0].length; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println();
}
String word = "ABCCED";
// algorithm requires that word is in uppercase if matrix contains upper case characters, vice versa
word = word.toUpperCase();
boolean exists = wordExists(board, word);
System.out.println(word + " exists : " + exists);
}
public static boolean wordExists(char[][] board, String word)
{
boolean exists = false;
// look for the first character of word in the matrix
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[0].length; j++)
{
if(board[i][j] == word.charAt(0))
{
int stringIndex = 1;
// update character to lower case so that we do not
// use the same character twice
board[i][j] = Character.toLowerCase(board[i][j]);
exists = wordExistsHelper(i, j, board, word, stringIndex);
// set character back to upper case to we can continue searching
board[i][j] = Character.toUpperCase(board[i][j]);
if(exists)
{
return true;
}
}
}
}
return exists;
}
public static boolean wordExistsHelper(int row, int col, char[][] board, String word, int stringIndex)
{
boolean exists = false;
int numRows = board.length;
int numColumns = board[0].length;
if(stringIndex < word.length())
{
// look left
if(col-1 >= 0 && board[row][col-1] == word.charAt(stringIndex))
{
// mark character so that we do not visit it twice
board[row][col-1] = Character.toLowerCase(board[row][col-1]);
exists = wordExistsHelper(row, col-1, board, word, stringIndex+1);
board[row][col-1] = Character.toUpperCase(board[row][col-1]);
}
// look right if the word does not exist
if(!exists && col+1 < numColumns && board[row][col+1] == word.charAt(stringIndex))
{
board[row][col+1] = Character.toLowerCase(board[row][col+1]);
exists = wordExistsHelper(row, col+1, board, word, stringIndex+1);
board[row][col+1] = Character.toUpperCase(board[row][col+1]);
}
// look up if word does not exist
if(!exists && row-1 >= 0 && board[row-1][col] == word.charAt(stringIndex))
{
board[row-1][col] = Character.toLowerCase(board[row-1][col]);
exists = wordExistsHelper(row-1, col, board, word, stringIndex+1);
board[row-1][col] = Character.toUpperCase(board[row-1][col]);
}
// look down if word does not exist
if(!exists && row+1 < numRows && board[row+1][col] == word.charAt(stringIndex))
{
board[row+1][col] = Character.toLowerCase(board[row+1][col]);
exists = wordExistsHelper(row+1, col, board, word, stringIndex+1);
board[row+1][col] = Character.toUpperCase(board[row+1][col]);
}
// at this point we can confirm that word does not exist
}
// return true if every single character in the word has been evaluated
else
{
exists = true;
}
return exists;
}
}