leetcode@ [79/140] Trie树应用 Word Search / Word Search II

时间:2023-03-09 04:05:09
leetcode@ [79/140] Trie树应用 Word Search / Word Search II

https://leetcode.com/problems/word-search/

class Solution {
public:
struct Trie{
Trie *next[];
bool isWord;
Trie() {
this->isWord = false;
for(auto &c: next) c = NULL;
}
};
void insert(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) p->next[idx] = new Trie();
p = p->next[idx];
}
p->isWord = true;
}
bool isPrefix(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) return false;
p = p->next[idx];
}
return true;
}
bool isWord(Trie *root, string word) {
Trie *p = root;
for(auto &c: word) {
int idx = c - 'A';
if(!p->next[idx]) return false;
p = p->next[idx];
}
return p->isWord;
}
bool check(vector<vector<char>> &board, int x, int y) {
int m = board.size(), n = board[].size();
if(x< || y< || x>=m || y>=n) return false;
return true;
} bool dfs(vector<vector<char>> &board, vector<vector<bool>> &vis, Trie *root, string s, int x, int y) {
if(isWord(root, s))
return true;
} int dir[][] = {{,}, {,}, {-,}, {,-}};
for(int i=;i<;++i) {
int nx = x + dir[i][], ny = y + dir[i][];
if(check(board, nx, ny) && !vis[nx][ny]) {
if(isPrefix(root, s + board[nx][ny])) {
vis[nx][ny] = true;
if(dfs(board, vis, root, s + board[nx][ny], nx, ny)) return true;
vis[nx][ny] = false;
}
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
if (board.empty() || board[].empty() || word.empty()) return false; string s = "";
int m = board.size(), n = board[].size();
vector<vector<bool>> vis(m, vector<bool>(n, false)); Trie *root = new Trie();
insert(root, word); for(int i=;i<m;++i) {
for(int j=;j<n;++j) {
//if(isWord(root, s + board[i][j])) return true;
if(isPrefix(root, s)) {
vis[i][j] = true;
if(dfs(board, vis, root, s + board[i][j], i, j)) return true;
vis[i][j] = false;
}
}
} return false;
}
};

https://leetcode.com/problems/word-search-ii/

struct TriNode {
TriNode *ch[];
bool isWord;
TriNode() : isWord(false) {
for (auto &a : ch) a = NULL;
}
};
class Solution {
public:
void insert(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) p->ch[i] = new TriNode();
p = p->ch[i];
}
p->isWord = true;
}
bool isPrefix(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return true;
}
bool isWord(TriNode *root, string word) {
TriNode *p = root;
for (auto &a : word) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return p->isWord;
}
bool isValid(vector<vector<char>> &board, int x, int y) {
int m = board.size(), n = board[].size();
if (x < || x >= m || y < || y >= n) return false;
return true;
}
void dfs(vector<vector<char>> board, vector<vector<int>> visit, set<string> &st, string s, TriNode *root, int x, int y) {
if(!isPrefix(root, s)) return;
if(isWord(root, s)) {
st.insert(s);
} int dx[] = {, -, , };
int dy[] = {, , , -};
int xx, yy;
for (int i = ; i < ; ++i) {
xx = x + dx[i]; yy = y + dy[i];
if (isValid(board, xx, yy) && !visit[xx][yy]) {
visit[xx][yy] = ;
dfs(board, visit, st, s + board[xx][yy], root, xx, yy);
visit[xx][yy] = ;
}
}
} vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res; res.clear();
if (board.empty() || board[].empty() || words.empty()) return res; TriNode *root = new TriNode();
for(auto &word : words) insert(root, word); int m = board.size(), n = board[].size();
vector<vector<int>> visit(m, vector<int>(n, )); string s="";
set<string> st; st.clear();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
visit[i][j] = ;
dfs(board, visit, st, s + board[i][j], root, i, j);
visit[i][j] = ;
}
} for (auto &word : st) res.push_back(word);
return res;
}
};