You are given an m x n matrix board, representing thecurrent state of a crossword puzzle. The crossword contains lowercase English letters (from solved words), ' ' to represent any empty cells, and '#' to represent any blocked cells.
A word can be placedhorizontally (left to right or right to left) or vertically (top to bottom or bottom to top) in the board if:
It does not occupy a cell containing the character '#'.
The cell each letter is placed in must either be ' ' (empty) or match the letter already on the board.
There must not be any empty cells ' ' or other lowercase letters directly left or right**** of the word if the word was placed horizontally.
There must not be any empty cells ' ' or other lowercase letters directly above or below the word if the word was placed vertically.
Given a string word, return trueifwordcan be placed inboard, orfalseotherwise.
Input: board =[["#"," ","#"],[" "," ","#"],["#","c"," "]], word ="abc"Output: trueExplanation: The word "abc" can be placed as shown above(top to bottom).
Input: board =[[" ","#","a"],[" ","#","c"],[" ","#","a"]], word ="ac"Output: falseExplanation: It is impossible to place the word because there will always be a space/letter above or below it.
Input: board =[["#"," ","#"],[" "," ","#"],["#"," ","c"]], word ="ca"Output: trueExplanation: The word "ca" can be placed as shown above(right to left).
A word can be placed in the crossword if it fits in any row or column (in either direction) and matches the crossword rules. We can scan all rows and columns, and for each possible slot, check if the word (or its reverse) can be placed there.
classSolution {
public:bool placeWordInCrossword(vector<vector<char>>& board, string word) {
int m = board.size(), n = board[0].size(), l = word.size();
auto check = [&](vector<char>& seg, string w) {
if (seg.size() != w.size()) return false;
for (int i =0; i < w.size(); ++i)
if (seg[i] !=' '&& seg[i] != w[i]) return false;
return true;
};
for (int i =0; i < m; ++i) {
for (int dir =0; dir <2; ++dir) {
vector<char> row = board[i];
if (dir) reverse(row.begin(), row.end());
for (int j =0; j <= n - l; ++j) {
if ((j ==0|| row[j-1] =='#') && (j + l == n || row[j+l] =='#')) {
vector<char> seg(row.begin() + j, row.begin() + j + l);
if (check(seg, word)) return true;
}
}
}
}
for (int j =0; j < n; ++j) {
for (int dir =0; dir <2; ++dir) {
vector<char> col(m);
for (int i =0; i < m; ++i) col[i] = board[i][j];
if (dir) reverse(col.begin(), col.end());
for (int i =0; i <= m - l; ++i) {
if ((i ==0|| col[i-1] =='#') && (i + l == m || col[i+l] =='#')) {
vector<char> seg(col.begin() + i, col.begin() + i + l);
if (check(seg, word)) return true;
}
}
}
}
return false;
}
};
classSolution {
publicbooleanplaceWordInCrossword(char[][] board, String word) {
int m = board.length, n = board[0].length, l = word.length();
for (int i = 0; i < m; ++i) {
for (int d = 0; d < 2; ++d) {
char[] row = board[i].clone();
if (d == 1) reverse(row);
for (int j = 0; j <= n - l; ++j) {
if ((j == 0 || row[j-1]=='#') && (j + l == n || row[j+l]=='#')) {
if (check(row, j, word)) returntrue;
}
}
}
}
for (int j = 0; j < n; ++j) {
for (int d = 0; d < 2; ++d) {
char[] col =newchar[m];
for (int i = 0; i < m; ++i) col[i]= board[i][j];
if (d == 1) reverse(col);
for (int i = 0; i <= m - l; ++i) {
if ((i == 0 || col[i-1]=='#') && (i + l == m || col[i+l]=='#')) {
if (check(col, i, word)) returntrue;
}
}
}
}
returnfalse;
}
privatebooleancheck(char[] arr, int start, String word) {
for (int i = 0; i < word.length(); ++i)
if (arr[start + i]!=' '&& arr[start + i]!= word.charAt(i)) returnfalse;
returntrue;
}
privatevoidreverse(char[] arr) {
for (int i = 0, j = arr.length- 1; i < j; ++i, --j) {
char t = arr[i]; arr[i]= arr[j]; arr[j]= t;
}
}
}
classSolution {
funplaceWordInCrossword(board: Array<CharArray>, word: String): Boolean {
val m = board.size; val n = board[0].size; val l = word.length
funcheck(arr: CharArray, start: Int, w: String): Boolean {
for (i in w.indices) if (arr[start + i] !=' '&& arr[start + i] != w[i]) returnfalsereturntrue }
funreverse(arr: CharArray) { var i = 0; var j = arr.size - 1; while (i < j) { val t = arr[i]; arr[i] = arr[j]; arr[j] = t; i++; j-- } }
for (i in0 until m) {
for (d in0..1) {
val row = board[i].clone()
if (d ==1) reverse(row)
for (j in0..n-l) {
if ((j ==0|| row[j-1] =='#') && (j + l == n || row[j+l] =='#')) {
if (check(row, j, word)) returntrue }
}
}
}
for (j in0 until n) {
for (d in0..1) {
val col = CharArray(m) { board[it][j] }
if (d ==1) reverse(col)
for (i in0..m-l) {
if ((i ==0|| col[i-1] =='#') && (i + l == m || col[i+l] =='#')) {
if (check(col, i, word)) returntrue }
}
}
}
returnfalse }
}
classSolution:
defplaceWordInCrossword(self, board: list[list[str]], word: str) -> bool:
m, n, l = len(board), len(board[0]), len(word)
defcheck(arr: list[str], w: str) -> bool:
return all(a ==' 'or a == b for a, b in zip(arr, w))
for rows in (board, list(zip(*board))):
for row in rows:
s =''.join(row)
for seg in s.split('#'):
if len(seg) == l:
if check(seg, word) or check(seg, word[::-1]):
returnTruereturnFalse
impl Solution {
pubfnplace_word_in_crossword(board: Vec<Vec<char>>, word: String) -> bool {
let m = board.len();
let n = board[0].len();
let l = word.len();
let check =|arr: &[char], w: &str| arr.len() == w.len() && arr.iter().zip(w.chars()).all(|(&a, b)| a ==' '|| a == b);
for rows in [board.clone(), (0..n).map(|j| (0..m).map(|i| board[i][j]).collect()).collect()] {
for row in rows {
let s: String = row.iter().collect();
for seg in s.split('#') {
if seg.len() == l {
let arr: Vec<char>= seg.chars().collect();
if check(&arr, &word) || check(&arr, &word.chars().rev().collect::<String>()) {
returntrue;
}
}
}
}
}
false }
}