bool exist(vector<vector<char>>& board, string word) {
int rows = board.size(), cols = board[0].size();
function<bool(int, int, int, set<string>)> dfs = [&](int r, int c, int k, set<string> visited) -> bool {
if (k == (int)word.size()) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
string key = to_string(r) + "," + to_string(c);
if (visited.count(key) || board[r][c] != word[k]) return false;
set<string> next = visited;
next.insert(key);
return dfs(r + 1, c, k + 1, next) || dfs(r - 1, c, k + 1, next) || dfs(r, c + 1, k + 1, next) || dfs(r, c - 1, k + 1, next);
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(r, c, 0, {})) return true;
}
}
return false;
}
public boolean exist(char[][] board, String word) {
int rows = board.length, cols = board[0].length;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(board, word, r, c, 0, new HashSet<>())) return true;
}
}
return false;
}
private boolean dfs(char[][] board, String word, int r, int c, int k, Set<String> visited) {
if (k == word.length()) return true;
if (r < 0 || c < 0 || r >= board.length || c >= board[0].length) return false;
String key = r + "," + c;
if (visited.contains(key) || board[r][c] != word.charAt(k)) return false;
Set<String> next = new HashSet<>(visited);
next.add(key);
return dfs(board, word, r + 1, c, k + 1, next) || dfs(board, word, r - 1, c, k + 1, next) || dfs(board, word, r, c + 1, k + 1, next) || dfs(board, word, r, c - 1, k + 1, next);
}
def exist(board, word):
rows, cols = len(board), len(board[0])
def dfs(r, c, k, visited):
if k == len(word): return True
if r < 0 or c < 0 or r >= rows or c >= cols: return False
key = (r, c)
if key in visited or board[r][c] != word[k]: return False
nxt = set(visited)
nxt.add(key)
return dfs(r + 1, c, k + 1, nxt) or dfs(r - 1, c, k + 1, nxt) or dfs(r, c + 1, k + 1, nxt) or dfs(r, c - 1, k + 1, nxt)
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0, set()): return True
return False
function exist(board: string[][], word: string): boolean {
const rows = board.length, cols = board[0].length;
const dfs = (r: number, c: number, k: number, visited: Set<string>): boolean => {
if (k === word.length) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
const key = r + ',' + c;
if (visited.has(key) || board[r][c] !== word[k]) return false;
const next = new Set(visited);
next.add(key);
return dfs(r + 1, c, k + 1, next) || dfs(r - 1, c, k + 1, next) || dfs(r, c + 1, k + 1, next) || dfs(r, c - 1, k + 1, next);
};
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (dfs(r, c, 0, new Set())) return true;
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
int rows = board.size(), cols = board[0].size();
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
function<bool(int, int, int)> dfs = [&](int r, int c, int k) -> bool {
if (k == (int)word.size()) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (visited[r][c] || board[r][c] != word[k]) return false;
visited[r][c] = true;
bool found = dfs(r + 1, c, k + 1) || dfs(r - 1, c, k + 1) || dfs(r, c + 1, k + 1) || dfs(r, c - 1, k + 1);
visited[r][c] = false;
return found;
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}
public boolean exist(char[][] board, String word) {
int rows = board.length, cols = board[0].length;
boolean[][] visited = new boolean[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(board, word, visited, r, c, 0)) return true;
}
}
return false;
}
private boolean dfs(char[][] board, String word, boolean[][] visited, int r, int c, int k) {
if (k == word.length()) return true;
if (r < 0 || c < 0 || r >= board.length || c >= board[0].length) return false;
if (visited[r][c] || board[r][c] != word.charAt(k)) return false;
visited[r][c] = true;
boolean found = dfs(board, word, visited, r + 1, c, k + 1) || dfs(board, word, visited, r - 1, c, k + 1) || dfs(board, word, visited, r, c + 1, k + 1) || dfs(board, word, visited, r, c - 1, k + 1);
visited[r][c] = false;
return found;
}
def exist(board, word):
rows, cols = len(board), len(board[0])
visited = [[False] * cols for _ in range(rows)]
def dfs(r, c, k):
if k == len(word): return True
if r < 0 or c < 0 or r >= rows or c >= cols: return False
if visited[r][c] or board[r][c] != word[k]: return False
visited[r][c] = True
found = dfs(r + 1, c, k + 1) or dfs(r - 1, c, k + 1) or dfs(r, c + 1, k + 1) or dfs(r, c - 1, k + 1)
visited[r][c] = False
return found
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0): return True
return False
function exist(board: string[][], word: string): boolean {
const rows = board.length, cols = board[0].length;
const visited = Array.from({ length: rows }, () => new Array<boolean>(cols).fill(false));
const dfs = (r: number, c: number, k: number): boolean => {
if (k === word.length) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (visited[r][c] || board[r][c] !== word[k]) return false;
visited[r][c] = true;
const found = dfs(r + 1, c, k + 1) || dfs(r - 1, c, k + 1) || dfs(r, c + 1, k + 1) || dfs(r, c - 1, k + 1);
visited[r][c] = false;
return found;
};
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
int rows = board.size(), cols = board[0].size();
int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
function<bool(int, int, int)> dfs = [&](int r, int c, int k) -> bool {
if (k == (int)word.size()) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (board[r][c] != word[k]) return false;
char saved = board[r][c];
board[r][c] = '#';
bool found = false;
for (auto& d : dirs) {
if (dfs(r + d[0], c + d[1], k + 1)) {
found = true;
break;
}
}
board[r][c] = saved;
return found;
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}
public boolean exist(char[][] board, String word) {
int rows = board.length, cols = board[0].length;
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(board, word, dirs, r, c, 0)) return true;
}
}
return false;
}
private boolean dfs(char[][] board, String word, int[][] dirs, int r, int c, int k) {
if (k == word.length()) return true;
if (r < 0 || c < 0 || r >= board.length || c >= board[0].length) return false;
if (board[r][c] != word.charAt(k)) return false;
char saved = board[r][c];
board[r][c] = '#';
boolean found = false;
for (int[] d : dirs) {
if (dfs(board, word, dirs, r + d[0], c + d[1], k + 1)) {
found = true;
break;
}
}
board[r][c] = saved;
return found;
}
def exist(board, word):
rows, cols = len(board), len(board[0])
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def dfs(r, c, k):
if k == len(word): return True
if r < 0 or c < 0 or r >= rows or c >= cols: return False
if board[r][c] != word[k]: return False
saved = board[r][c]
board[r][c] = '#'
found = False
for dr, dc in dirs:
if dfs(r + dr, c + dc, k + 1):
found = True
break
board[r][c] = saved
return found
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0): return True
return False
function exist(board: string[][], word: string): boolean {
const rows = board.length, cols = board[0].length;
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const dfs = (r: number, c: number, k: number): boolean => {
if (k === word.length) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (board[r][c] !== word[k]) return false;
const saved = board[r][c];
board[r][c] = '#';
let found = false;
for (const [dr, dc] of dirs) {
if (dfs(r + dr, c + dc, k + 1)) {
found = true;
break;
}
}
board[r][c] = saved;
return found;
};
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
int rows = board.size(), cols = board[0].size();
if ((int)word.size() > rows * cols) return false;
map<char, int> count;
for (auto& row : board) {
for (char ch : row) count[ch]++;
}
map<char, int> need;
for (char ch : word) need[ch]++;
for (auto& [ch, n] : need) {
if (count[ch] < n) return false;
}
if (count[word.front()] > count[word.back()]) {
reverse(word.begin(), word.end());
}
function<bool(int, int, int)> dfs = [&](int r, int c, int k) -> bool {
if (board[r][c] != word[k]) return false;
if (k == (int)word.size() - 1) return true;
char saved = board[r][c];
board[r][c] = '#';
bool found =
(r > 0 && dfs(r - 1, c, k + 1)) ||
(c > 0 && dfs(r, c - 1, k + 1)) ||
(r + 1 < rows && dfs(r + 1, c, k + 1)) ||
(c + 1 < cols && dfs(r, c + 1, k + 1));
board[r][c] = saved;
return found;
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}
public boolean exist(char[][] board, String word) {
int rows = board.length, cols = board[0].length;
if (word.length() > rows * cols) return false;
Map<Character, Integer> count = new HashMap<>();
for (char[] row : board) {
for (char ch : row) count.merge(ch, 1, Integer::sum);
}
Map<Character, Integer> need = new HashMap<>();
for (char ch : word.toCharArray()) need.merge(ch, 1, Integer::sum);
for (Map.Entry<Character, Integer> e : need.entrySet()) {
if (count.getOrDefault(e.getKey(), 0) < e.getValue()) return false;
}
if (count.getOrDefault(word.charAt(0), 0) > count.getOrDefault(word.charAt(word.length() - 1), 0)) {
word = new StringBuilder(word).reverse().toString();
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (dfs(board, word, r, c, 0)) return true;
}
}
return false;
}
private boolean dfs(char[][] board, String word, int r, int c, int k) {
if (board[r][c] != word.charAt(k)) return false;
if (k == word.length() - 1) return true;
char saved = board[r][c];
board[r][c] = '#';
boolean found =
(r > 0 && dfs(board, word, r - 1, c, k + 1)) ||
(c > 0 && dfs(board, word, r, c - 1, k + 1)) ||
(r + 1 < board.length && dfs(board, word, r + 1, c, k + 1)) ||
(c + 1 < board[0].length && dfs(board, word, r, c + 1, k + 1));
board[r][c] = saved;
return found;
}
def exist(board, word):
rows, cols = len(board), len(board[0])
if len(word) > rows * cols: return False
count = {}
for row in board:
for ch in row: count[ch] = count.get(ch, 0) + 1
need = {}
for ch in word: need[ch] = need.get(ch, 0) + 1
for ch, n in need.items():
if count.get(ch, 0) < n: return False
if count.get(word[0], 0) > count.get(word[-1], 0):
word = word[::-1]
def dfs(r, c, k):
if board[r][c] != word[k]: return False
if k == len(word) - 1: return True
saved = board[r][c]
board[r][c] = '#'
found = ((r > 0 and dfs(r - 1, c, k + 1)) or
(c > 0 and dfs(r, c - 1, k + 1)) or
(r + 1 < rows and dfs(r + 1, c, k + 1)) or
(c + 1 < cols and dfs(r, c + 1, k + 1)))
board[r][c] = saved
return found
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0): return True
return False
function exist(board: string[][], word: string): boolean {
const rows = board.length, cols = board[0].length;
if (word.length > rows * cols) return false;
const count = new Map<string, number>();
for (const row of board) {
for (const ch of row) count.set(ch, (count.get(ch) ?? 0) + 1);
}
const need = new Map<string, number>();
for (const ch of word) need.set(ch, (need.get(ch) ?? 0) + 1);
for (const [ch, n] of need) {
if ((count.get(ch) ?? 0) < n) return false;
}
if ((count.get(word[0]) ?? 0) > (count.get(word[word.length - 1]) ?? 0)) {
word = [...word].reverse().join('');
}
const dfs = (r: number, c: number, k: number): boolean => {
if (board[r][c] !== word[k]) return false;
if (k === word.length - 1) return true;
const saved = board[r][c];
board[r][c] = '#';
const found =
(r > 0 && dfs(r - 1, c, k + 1)) ||
(c > 0 && dfs(r, c - 1, k + 1)) ||
(r + 1 < rows && dfs(r + 1, c, k + 1)) ||
(c + 1 < cols && dfs(r, c + 1, k + 1));
board[r][c] = saved;
return found;
};
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}
return false;
}