Skip to content

Commit 9e788b0

Browse files
Word Search II
1 parent fc6ed47 commit 9e788b0

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

30.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
3+
Word Search II
4+
--------------
5+
6+
Given a 2D board and a list of words from the dictionary, find all words in the board.
7+
8+
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.
9+
10+
11+
12+
Example:
13+
14+
Input:
15+
board = [
16+
['o','a','a','n'],
17+
['e','t','a','e'],
18+
['i','h','k','r'],
19+
['i','f','l','v']
20+
]
21+
words = ["oath","pea","eat","rain"]
22+
23+
Output: ["eat","oath"]
24+
25+
26+
Note:
27+
28+
All inputs are consist of lowercase letters a-z.
29+
The values of words are distinct.
30+
31+
*/
32+
33+
class Solution {
34+
class Trie{
35+
public:
36+
Trie *children[26]; // pointers to its substrings starting with 'a' to 'z'
37+
bool leaf; // if the node is a leaf, or if there is a word stopping at here
38+
int idx; // if it is a leaf, the string index of the array words
39+
Trie()
40+
{
41+
this->leaf = false;
42+
this->idx = 0;
43+
fill_n(this->children, 26, nullptr);
44+
}
45+
};
46+
47+
public:
48+
void insertWords(Trie *root, vector<string>& words, int idx)
49+
{
50+
int pos = 0, len = words[idx].size();
51+
while(pos<len)
52+
{
53+
if(nullptr == root->children[words[idx][pos]-'a']) root->children[words[idx][pos]-'a'] = new Trie();
54+
root = root->children[words[idx][pos++]-'a'];
55+
}
56+
root->leaf = true;
57+
root->idx = idx;
58+
}
59+
60+
Trie *buildTrie(vector<string>& words)
61+
{
62+
Trie *root = new Trie();
63+
int i;
64+
for(i=0; i<words.size();i++) insertWords(root, words, i);
65+
return root;
66+
}
67+
68+
void checkWords(vector<vector<char>>& board, int i, int j, int row, int col, Trie *root, vector<string> &res, vector<string>& words)
69+
{
70+
char temp;
71+
if(board[i][j]=='X') return; // visited before;
72+
if(nullptr == root->children[board[i][j]-'a']) return ; // no string with such prefix
73+
else
74+
{
75+
temp = board[i][j];
76+
if(root->children[temp-'a']->leaf) // if it is a leaf
77+
{
78+
res.push_back(words[root->children[temp-'a']->idx]);
79+
root->children[temp-'a']->leaf = false; // set to false to indicate that we found it already
80+
}
81+
board[i][j]='X'; //mark the current position as visited
82+
// check all the possible neighbors
83+
if(i>0) checkWords(board, i-1, j, row, col, root->children[temp-'a'], res, words);
84+
if((i+1)<row) checkWords(board, i+1, j, row, col, root->children[temp-'a'], res, words);
85+
if(j>0) checkWords(board, i, j-1, row, col, root->children[temp-'a'], res, words);
86+
if((j+1)<col) checkWords(board, i, j+1, row, col, root->children[temp-'a'], res, words);
87+
board[i][j] = temp; // recover the current position
88+
}
89+
}
90+
91+
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
92+
vector<string> res;
93+
int row = board.size();
94+
if(0==row) return res;
95+
int col = board[0].size();
96+
if(0==col) return res;
97+
int wordCount = words.size();
98+
if(0==wordCount) return res;
99+
100+
Trie *root = buildTrie(words);
101+
102+
int i,j;
103+
for(i =0 ; i<row; i++)
104+
{
105+
for(j=0; j<col && wordCount > res.size(); j++)
106+
{
107+
checkWords(board, i, j, row, col, root, res, words);
108+
}
109+
}
110+
return res;
111+
}
112+
};

0 commit comments

Comments
 (0)