-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
wordSearch.II.cpp
141 lines (122 loc) · 4.02 KB
/
wordSearch.II.cpp
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
134
135
136
137
138
139
140
// Source : https://leetcode.com/problems/word-search-ii/
// Author : Hao Chen
// Date : 2015-06-10
/**********************************************************************************
*
* Given a 2D board and a list of words from the dictionary, find all words in the board.
*
* Each word must 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 in a word.
*
* For example,
* Given words = ["oath","pea","eat","rain"] and board =
*
* [
* ['o','a','a','n'],
* ['e','t','a','e'],
* ['i','h','k','r'],
* ['i','f','l','v']
* ]
*
* Return ["eat","oath"].
*
* Note:
* You may assume that all inputs are consist of lowercase letters a-z.
*
* click to show hint.
*
* You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier?
*
* If the current candidate does not exist in all words' prefix, you could stop backtracking immediately.
* What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not?
* How about a Trie? If you would like to learn how to implement a basic trie, please work on this problem:
* Implement Trie (Prefix Tree) first.
*
**********************************************************************************/
const int MAX_CHARS = 26;
class TrieNode {
public:
TrieNode(string s):isWord(false), word(s) {
memset(children, 0, sizeof(children));
}
TrieNode* & operator [] (char ch) {
return children[(ch - 'a') % MAX_CHARS];
}
TrieNode* & operator [] (int idx) {
return children[idx % MAX_CHARS];
}
public:
string word;
bool isWord;
private:
TrieNode* children[MAX_CHARS];
};
class TrieTree {
public:
TrieTree():root(new TrieNode("")) { }
~TrieTree() { freeTree(root); }
TrieNode* getRoot() {
return root;
}
void addWord(string& s){
TrieNode *node = root;
string t;
for (int i=0; i<s.size(); i++){
t += s[i];
if ( (*node)[s[i]] == NULL ){
(*node)[s[i]] = new TrieNode(t);
}
node = (*node)[s[i]];
}
node->isWord = true;
}
private:
void freeTree(TrieNode* node){
for(int i=0; i<MAX_CHARS; i++){
if ((*node)[i]!=NULL){
freeTree((*node)[i]);
}
}
delete node;
}
TrieNode *root;
};
class Solution {
public:
void findWords(vector<vector<char>>& board, TrieNode* root, int row, int col, vector<string>& result){
if (row < 0 || col < 0 ||
row >= board.size() ||
col >= board[row].size() ||
board[row][col] == '\0' ) {
return;
}
char ch = board[row][col];
root = (*root)[ch];
if (root==NULL) return;
if (root->isWord){
result.push_back(root->word);
root->isWord = false;
}
board[row][col] = '\0';
findWords(board, root, row, col - 1, result);
findWords(board, root, row, col + 1, result);
findWords(board, root, row + 1, col, result);
findWords(board, root, row - 1, col, result);
board[row][col] = ch;
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
TrieTree t;
for (int i = 0; i<words.size(); i++){
t.addWord(words[i]);
}
vector<string> result;
for (int i = 0; i<board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
findWords(board, t.getRoot(), i, j, result);
}
}
return result;
}
};