Skip to content

Commit 4fa86d2

Browse files
add 2018
1 parent 9ff09b3 commit 4fa86d2

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2018|[Check if Word Can Be Placed In Crossword](https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2018.java) ||Medium||
1112
|2012|[Sum of Beauty in the Array](https://leetcode.com/problems/sum-of-beauty-in-the-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2012.java) ||Medium||
1213
|2011|[Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2011.java) ||Easy||
1314
|2007|[Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2007.java) ||Medium||
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2018 {
4+
public static class Solution1 {
5+
public boolean placeWordInCrossword(char[][] board, String word) {
6+
int m = board.length;
7+
int n = board[0].length;
8+
for (int i = 0; i < m; i++) {
9+
for (int j = 0; j < n; j++) {
10+
if (board[i][j] == ' ' || board[i][j] == word.charAt(0)) {
11+
if (canPlaceTopDown(word, board, i, j) || canPlaceLeftRight(word, board, i, j)
12+
|| canPlaceBottomUp(word, board, i, j) || canPlaceRightLeft(word, board, i, j)) {
13+
return true;
14+
}
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
private boolean canPlaceRightLeft(String word, char[][] board, int row, int col) {
22+
if (col + 1 < board[0].length && (Character.isLowerCase(board[row][col + 1]) || board[row][col + 1] == ' ')) {
23+
return false;
24+
}
25+
int k = 0;
26+
int j = col;
27+
for (; j >= 0 && k < word.length(); j--) {
28+
if (board[row][j] != word.charAt(k) && board[row][j] != ' ') {
29+
return false;
30+
} else {
31+
k++;
32+
}
33+
}
34+
return k == word.length() && (j < 0 || board[row][j] == '#');
35+
}
36+
37+
private boolean canPlaceBottomUp(String word, char[][] board, int row, int col) {
38+
if (row + 1 < board.length && (Character.isLowerCase(board[row + 1][col]) || board[row + 1][col] == ' ')) {
39+
return false;
40+
}
41+
int k = 0;
42+
int i = row;
43+
for (; i >= 0 && k < word.length(); i--) {
44+
if (board[i][col] != word.charAt(k) && board[i][col] != ' ') {
45+
return false;
46+
} else {
47+
k++;
48+
}
49+
}
50+
return k == word.length() && (i < 0 || board[i][col] == '#');
51+
}
52+
53+
private boolean canPlaceLeftRight(String word, char[][] board, int row, int col) {
54+
if (col > 0 && (Character.isLowerCase(board[row][col - 1]) || board[row][col - 1] == ' ')) {
55+
return false;
56+
}
57+
int k = 0;
58+
int j = col;
59+
for (; j < board[0].length && k < word.length(); j++) {
60+
if (board[row][j] != word.charAt(k) && board[row][j] != ' ') {
61+
return false;
62+
} else {
63+
k++;
64+
}
65+
}
66+
return k == word.length() && (j == board[0].length || board[row][j] == '#');
67+
}
68+
69+
private boolean canPlaceTopDown(String word, char[][] board, int row, int col) {
70+
if (row > 0 && (Character.isLowerCase(board[row - 1][col]) || board[row - 1][col] == ' ')) {
71+
return false;
72+
}
73+
int k = 0;
74+
int i = row;
75+
for (; i < board.length && k < word.length(); i++) {
76+
if (board[i][col] != word.charAt(k) && board[i][col] != ' ') {
77+
return false;
78+
} else {
79+
k++;
80+
}
81+
}
82+
return k == word.length() && (i == board.length || board[i][col] == '#');
83+
}
84+
}
85+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2018;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2018Test {
10+
private static _2018.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2018.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.placeWordInCrossword(new char[][]{
20+
{'#', ' ', '#'},
21+
{' ', ' ', '#'},
22+
{'#', 'c', ' '}
23+
}, "abc"));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(false, solution1.placeWordInCrossword(new char[][]{
29+
{' ', '#', 'a'},
30+
{' ', '#', 'c'},
31+
{' ', '#', 'a'}
32+
}, "ac"));
33+
}
34+
35+
@Test
36+
public void test3() {
37+
assertEquals(true, solution1.placeWordInCrossword(new char[][]{
38+
{'#', ' ', '#'},
39+
{' ', ' ', '#'},
40+
{'#', ' ', 'c'}
41+
}, "ca"));
42+
}
43+
44+
@Test
45+
public void test4() {
46+
assertEquals(true, solution1.placeWordInCrossword(new char[][]{
47+
{'#', ' ', '#'},
48+
{' ', ' ', '#'},
49+
{'#', ' ', 'c'}
50+
}, "cd"));
51+
}
52+
53+
@Test
54+
public void test5() {
55+
assertEquals(true, solution1.placeWordInCrossword(new char[][]{
56+
{'#', ' ', '#'},
57+
{' ', '#', '#'},
58+
{'#', ' ', 'c'}
59+
}, "ca"));
60+
}
61+
62+
@Test
63+
public void test6() {
64+
assertEquals(true, solution1.placeWordInCrossword(new char[][]{
65+
{'#', ' ', '#'},
66+
{'#', 'c', '#'},
67+
{'#', '#', 'c'}
68+
}, "ca"));
69+
}
70+
71+
@Test
72+
public void test7() {
73+
assertEquals(false, solution1.placeWordInCrossword(new char[][]{
74+
{' ', 'b', '#'},
75+
{' ', '#', '#'},
76+
{' ', ' ', 'c'}
77+
}, "ca"));
78+
}
79+
80+
}

0 commit comments

Comments
 (0)