LeetCode 2018: Check if Word Can Be Placed In Crossword Solution
Master LeetCode problem 2018 (Check if Word Can Be Placed In Crossword), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2018. Check if Word Can Be Placed In Crossword
Problem Explanation
Explanation
To solve this problem, we can iterate through all possible starting positions on the board and check if the word can be placed horizontally or vertically from that position. We need to ensure that the word can fit in the given direction and that the surrounding cells are either empty or match the letters of the word.
- For each starting position (cell), we check if we can place the word horizontally or vertically.
- To check if the word can be placed horizontally, we ensure that there is enough space to the right and left of the starting cell for the word length, and that the surrounding cells are either empty or match the word's letters.
- To check if the word can be placed vertically, we ensure that there is enough space above and below the starting cell for the word length, and that the surrounding cells are either empty or match the word's letters.
- If we find a valid position where the word can be placed, we return true. If no such position is found, we return false.
Time Complexity: O(m * n * max(m, n)) where m is the number of rows, n is the number of columns in the board.
Space Complexity: O(1)
Solution Code
class Solution {
public boolean placeWordInCrossword(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (canPlaceWord(board, word, i, j, 1, 0) || canPlaceWord(board, word, i, j, 0, 1)) {
return true;
}
}
}
return false;
}
private boolean canPlaceWord(char[][] board, String word, int row, int col, int dx, int dy) {
int m = board.length;
int n = board[0].length;
for (int i = 0; i < word.length(); i++) {
if (row < 0 || row >= m || col < 0 || col >= n || (board[row][col] != ' ' && board[row][col] != word.charAt(i))) {
return false;
}
row += dx;
col += dy;
}
return (row == m || row == -1 || col == n || col == -1 || board[row - dx][col - dy] == '#');
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2018 (Check if Word Can Be Placed In Crossword)?
This page provides optimized solutions for LeetCode problem 2018 (Check if Word Can Be Placed In Crossword) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2018 (Check if Word Can Be Placed In Crossword)?
The time complexity for LeetCode 2018 (Check if Word Can Be Placed In Crossword) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2018 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2018 in Java, C++, or Python.