Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 635885b

Browse files
committed
Add solution #2018
1 parent ed4df82 commit 635885b

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,689 LeetCode solutions in JavaScript
1+
# 1,690 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1548,6 +1548,7 @@
15481548
2014|[Longest Subsequence Repeated k Times](./solutions/2014-longest-subsequence-repeated-k-times.js)|Hard|
15491549
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15501550
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
1551+
2018|[Check if Word Can Be Placed In Crossword](./solutions/2018-check-if-word-can-be-placed-in-crossword.js)|Medium|
15511552
2027|[Minimum Moves to Convert String](./solutions/2027-minimum-moves-to-convert-string.js)|Easy|
15521553
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
15531554
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* 2018. Check if Word Can Be Placed In Crossword
3+
* https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/
4+
* Difficulty: Medium
5+
*
6+
* You are given an m x n matrix board, representing the current state of a crossword puzzle.
7+
* The crossword contains lowercase English letters (from solved words), ' ' to represent any
8+
* empty cells, and '#' to represent any blocked cells.
9+
*
10+
* A word can be placed horizontally (left to right or right to left) or vertically (top to
11+
* bottom or bottom to top) in the board if:
12+
* - It does not occupy a cell containing the character '#'.
13+
* - The cell each letter is placed in must either be ' ' (empty) or match the letter already
14+
* on the board.
15+
* - There must not be any empty cells ' ' or other lowercase letters directly left or right
16+
* of the word if the word was placed horizontally.
17+
* - There must not be any empty cells ' ' or other lowercase letters directly above or below
18+
* the word if the word was placed vertically.
19+
*
20+
* Given a string word, return true if word can be placed in board, or false otherwise.
21+
*/
22+
23+
/**
24+
* @param {character[][]} board
25+
* @param {string} word
26+
* @return {boolean}
27+
*/
28+
var placeWordInCrossword = function(board, word) {
29+
const rows = board.length;
30+
const cols = board[0].length;
31+
const wordLen = word.length;
32+
33+
function canPlace(row, col, dr, dc) {
34+
for (let i = 0; i < wordLen; i++) {
35+
const r = row + i * dr;
36+
const c = col + i * dc;
37+
if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] === '#') return false;
38+
if (board[r][c] !== ' ' && board[r][c] !== word[i]) return false;
39+
}
40+
41+
const beforeR = row - dr;
42+
const beforeC = col - dc;
43+
const afterR = row + wordLen * dr;
44+
const afterC = col + wordLen * dc;
45+
46+
if ((beforeR >= 0 && beforeR < rows && beforeC >= 0
47+
&& beforeC < cols && board[beforeR][beforeC] !== '#')
48+
|| (afterR >= 0 && afterR < rows && afterC >= 0
49+
&& afterC < cols && board[afterR][afterC] !== '#')) {
50+
return false;
51+
}
52+
53+
return true;
54+
}
55+
56+
for (let r = 0; r < rows; r++) {
57+
for (let c = 0; c < cols; c++) {
58+
if (canPlace(r, c, 0, 1) || canPlace(r, c, 0, -1)
59+
|| canPlace(r, c, 1, 0) || canPlace(r, c, -1, 0)) {
60+
return true;
61+
}
62+
}
63+
}
64+
65+
return false;
66+
};

0 commit comments

Comments
 (0)