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”].
Note:
You may assume that all inputs are consist of lowercase letters a-z.
typedef pair<int, int> pint;
auto isLeftOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
if (R < 1) return false;
else if ( !vvb[L][R-1] && c == board[L][R-1]) return true;
else return false;
};
auto isRightOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
if (R+1 >= board[0].size()) return false;
else if ( !vvb[L][R+1] && c == board[L][R+1]) return true;
else return false;
};
auto isUpOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
if (L < 1) return false;
else if ( !vvb[L-1][R] && c == board[L-1][R]) return true;
else return false;
};
auto isDownOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
if (L+1 >= board.size()) return false;
else if ( !vvb[L+1][R] && c == board[L+1][R]) return true;
else return false;
};
class Solution {
public:
unordered_multimap<char, pair<int, int>> mmps;
typedef pair<int,int> pint;
bool isIn(vector<vector<char>>& board, string &word, size_t index, size_t L, size_t R, vector<vector<bool>> &vvb) {
if (index >= word.size()) return true;
if (isLeftOK(L, R, word[index], board, vvb)) {
vvb[L][R-1] = true;
if( isIn(board, word, index+1, L, R-1, vvb))
{
vvb[L][R-1] = false;
return true;
}
vvb[L][R-1] = false;
}
if (isRightOK(L, R, word[index], board, vvb)) {
vvb[L][R+1] = true;
if (isIn(board, word, index+1, L, R+1, vvb)) {
vvb[L][R+1] = false;
return true;
}
vvb[L][R+1] = false;
}
if (isUpOK(L, R, word[index], board, vvb)) {
vvb[L-1][R] = true;
if (isIn(board, word, index+1, L-1, R, vvb)) {
vvb[L-1][R] = false;
return true;
}
vvb[L-1][R] = false;
}
if (isDownOK(L, R, word[index], board, vvb)) {
vvb[L+1][R] = true;
if (isIn(board, word, index+1, L+1, R, vvb)) {
vvb[L+1][R] = false;
return true;
}
vvb[L+1][R] = false;
}
return false;
}
//
bool isInBoard(vector<vector<char>>& board, string &word) {
string word0(word.rbegin(), word.rend());
bool res;
if (mmps.empty()) {
for (size_t i = 0; i < board.size(); ++i) {
for (size_t j = 0; j < board[i].size(); ++j) {
mmps.insert(pair<char, pint>(board[i][j], pint(i,j)));
}
}
}
vector<vector<bool>> vvb(board.size(), vector<bool>(board[0].size(), false));
// forward
char c = word[0];
auto resfs = mmps.equal_range(c);
for (auto it = resfs.first; it != resfs.second; it++) {
res = false;
vvb[it->second.first][it->second.second] = true;
res = isIn(board, word, 1, it->second.first, it->second.second, vvb);
vvb[it->second.first][it->second.second] = false;
if (res) return res;
}
// reverse
c = word0[0];
resfs = mmps.equal_range(c);
for (auto it = resfs.first; it != resfs.second; it++) {
res = false;
vvb[it->second.first][it->second.second] = true;
res = isIn(board, word0, 1, it->second.first, it->second.second, vvb);
vvb[it->second.first][it->second.second] = false;
if (res) return res;
}
return false;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
unordered_set<string> s1(words.begin(), words.end());
vector<string> words0(s1.begin(), s1.end());
for (auto &i : words0) {
if (isInBoard(board, i))
res.push_back(i);
}
return res;
}
};