Leetcode (212) Word Search II
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”].
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
root = new TrieNode();
vector<string> ans;
for (auto word: words)
{
insert(word);
}
int rows=board.size();
if (rows == 0) return ans;
int cols=board[0].size();
for (int i=0;ifor (int j=0;jif (root->next[board[i][j]-'a'])
{
string word=string(1, board[i][j]);
if (isWord(word)) ans.push_back(word);
vis[i][j]=true;
dfs(i, j, board, ans, word);
vis[i][j]=false;
}
}
}
sort(ans.begin(), ans.end());
auto last = unique(ans.begin(), ans.end());
return vector<string>(ans.begin(), last);
}
private:
static const int dir[][2];
bool vis[100][100];
void dfs(int x, int y, const vector<vector<char>>& board, vector<string>& ans, const string& word)
{
int rows=board.size(), cols=board[0].size();
for (int i=0;i<4;++i)
{
int nx=x+dir[i][0], ny=y+dir[i][1];
if (nx<0 || ny<0 || nx>=rows || ny>=cols || vis[nx][ny]) continue;
string newWord = word+board[nx][ny];
if (hasPath(newWord))
{
if (isWord(newWord))
{
ans.push_back(newWord);
}
vis[nx][ny]=true;
dfs(nx, ny, board, ans, newWord);
vis[nx][ny]=false;
}
}
}
struct TrieNode
{
bool isEnd;
TrieNode* next[26];
TrieNode(bool is=false)
{
isEnd = is;
memset(next, 0, sizeof(next));
}
};
TrieNode* root;
void free(TrieNode* node)
{
if (!node) return;
for (int i=0;i<26;++i)
{
free(node->next[i]);
}
}
void insert(const string& word)
{
TrieNode* node=root;
for (int i=0;iint idx = word[i]-'a';
if (!node->next[idx])
{
node->next[idx] = new TrieNode();
}
node = node->next[idx];
if (i == word.length()-1) node->isEnd=true;
}
}
bool hasPath(const string& word)
{
TrieNode* node=root;
for (int i=0;iint idx = word[i]-'a';
if (node->next[idx])
{
node = node->next[idx];
}
else
{
return false;
}
}
return true;
}
bool isWord(const string& word)
{
TrieNode* node=root;
for (int i=0;iint idx = word[i]-'a';
if (node->next[idx])
{
node = node->next[idx];
}
else
{
return false;
}
}
return node->isEnd;
}
};
const int Solution::dir[][2] ={1,0,0,1,-1,0,0,-1};