-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWordSearch2.java
133 lines (122 loc) · 5 KB
/
WordSearch2.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
132
133
/*https://leetcode.com/problems/word-search-ii/*/
class TrieNode
{
TrieNode[] hash;
boolean isEndOfWord;
TrieNode()
{
hash = new TrieNode[26];
isEndOfWord = false;
}
}
class Trie
{
boolean stopDFS = false;
TrieNode root;
Trie()
{
root = new TrieNode();
}
public void add(String s)
{
TrieNode temp = root;
for (int i = 0; i < s.length(); ++i)
{
if (temp.hash[s.charAt(i)-'a'] == null)
temp.hash[s.charAt(i)-'a'] = new TrieNode();
temp = temp.hash[s.charAt(i)-'a'];
}
temp.isEndOfWord = true;
}
public boolean checkPresence(TrieNode pointer, char ch)
{
if (pointer.hash[ch-'a'] != null)
return true;
return false;
}
}
class Solution {
boolean visited[][];
Set<String> resultSet;
public List<String> findWords(char[][] board, String[] words) {
//add all words to a trie
Trie trie = new Trie();
for (String word : words)
trie.add(word);
resultSet = new HashSet<String>();
List<String> resultList = new ArrayList<String>();
boolean result = false; //store false in result
visited = new boolean[board.length][board[0].length];
StringBuffer currentWord = new StringBuffer("");
for (int r = 0; r < board.length; ++r)
{
for (int c = 0; c < board[r].length; ++c)
{
TrieNode pointer = trie.root;
if (trie.checkPresence(pointer,board[r][c])) //if current cell contains any of the first characters
{
visited[r][c] = true; //mark the cell as visited
currentWord.append(board[r][c]); //append the character to current word
runDFS(trie, board, r, c, 1, currentWord, pointer); //recursion call to DFS
visited[r][c] = false; // BACKTRACK
currentWord.setLength(currentWord.length()-1); // BACKTRACK
}
}
}
//add words from set to list
for (String str : resultSet)
resultList.add(str);
return resultList; //this definitely returns false
}
public void runDFS(Trie trie, char[][] board, int r, int c, int index, StringBuffer currentWord, TrieNode pointer)
{
TrieNode ref = pointer; //store the current trie pointer in a reference
pointer = pointer.hash[currentWord.charAt(currentWord.length()-1)-'a']; //move the pointer to the next node
//base case
if (pointer.isEndOfWord) //if the current point is the end of a word
{
resultSet.add(currentWord.toString()); //add it to set
//check if further paths are available
boolean isTrieAvailable = false;
for (int i = 0; i < 26; ++i)
if (pointer.hash[i] != null)
isTrieAvailable = true;
//if the current node is a dead end, return
if (!isTrieAvailable)
return;
}
if (c+1 < board[0].length && trie.checkPresence(pointer,board[r][c+1]) && !visited[r][c+1]) //for right cell
{
visited[r][c+1] = true; //mark as visited
currentWord.append(board[r][c+1]); //append the character to current word
runDFS(trie, board, r, c+1, index+1, currentWord, pointer); //recursion call to DFS
visited[r][c+1] = false; // BACKTRACK
currentWord.setLength(currentWord.length()-1); // BACKTRACK
}
if (c-1 >= 0 && trie.checkPresence(pointer,board[r][c-1]) && !visited[r][c-1]) //for left cell
{
visited[r][c-1] = true; //mark as visited
currentWord.append(board[r][c-1]); //append the character to current word
runDFS(trie, board, r, c-1, index+1, currentWord, pointer); //recursion call to DFS
visited[r][c-1] = false; // BACKTRACK
currentWord.setLength(currentWord.length()-1); // BACKTRACK
}
if (r+1 < board.length && trie.checkPresence(pointer,board[r+1][c]) && !visited[r+1][c]) //for down cell
{
visited[r+1][c] = true; //mark as visited
currentWord.append(board[r+1][c]); //append the character to current word
runDFS(trie, board, r+1, c, index+1, currentWord, pointer); //recursion call to DFS
visited[r+1][c] = false; // BACKTRACK
currentWord.setLength(currentWord.length()-1); // BACKTRACK
}
if (r-1 >= 0 && trie.checkPresence(pointer,board[r-1][c]) && !visited[r-1][c]) //for up cell
{
visited[r-1][c] = true; //mark as visited
currentWord.append(board[r-1][c]); //append the character to current word
runDFS(trie, board, r-1, c, index+1, currentWord, pointer); //recursion call to DFS
visited[r-1][c] = false; // BACKTRACK
currentWord.setLength(currentWord.length()-1); // BACKTRACK
}
pointer = ref; // BACKTRACK
}
}