Skip to content

Commit 6208d8f

Browse files
authored
Merge pull request exercism#617 from exercism/word-search
word-search: add to track
2 parents e32ba10 + 853ae5f commit 6208d8f

9 files changed

Lines changed: 528 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@
255255
"difficulty": 7,
256256
"topics": []
257257
},
258+
{
259+
"slug": "word-search",
260+
"difficulty": 7,
261+
"topics": []
262+
},
258263
{
259264
"slug": "simple-linked-list",
260265
"difficulty": 7,

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ include 'triangle'
6969
include 'trinary'
7070
include 'twelve-days'
7171
include 'word-count'
72+
include 'word-search'
7273
include 'wordy'

‎exercises/word-search/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Pair {
2+
3+
private final int x;
4+
5+
private final int y;
6+
7+
Pair(final int x, final int y) {
8+
this.y = y;
9+
this.x = x;
10+
}
11+
12+
int getX() {
13+
return x;
14+
}
15+
16+
int getY() {
17+
return y;
18+
}
19+
20+
@Override
21+
public boolean equals(final Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
25+
Pair pair = (Pair) o;
26+
27+
return x == pair.x && y == pair.y;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
int result = x;
33+
result = 31 * result + y;
34+
return result;
35+
}
36+
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class WordLocation {
2+
3+
private final Pair startCoord;
4+
5+
private final Pair endCoord;
6+
7+
WordLocation(final Pair startCoord, final Pair endCoord) {
8+
this.startCoord = startCoord;
9+
this.endCoord = endCoord;
10+
}
11+
12+
@Override
13+
public boolean equals(final Object o) {
14+
if (this == o) return true;
15+
if (o == null || getClass() != o.getClass()) return false;
16+
17+
WordLocation that = (WordLocation) o;
18+
19+
return startCoord.equals(that.startCoord) && endCoord.equals(that.endCoord);
20+
}
21+
22+
@Override
23+
public int hashCode() {
24+
int result = startCoord.hashCode();
25+
result = 31 * result + endCoord.hashCode();
26+
return result;
27+
}
28+
29+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.*;
2+
import java.util.function.Function;
3+
import java.util.stream.Collectors;
4+
5+
class WordSearcher {
6+
7+
private static final List<Pair> DIRECTIONS = Arrays.asList(
8+
new Pair( 1, 0), // N
9+
new Pair( 1, 1), // NE
10+
new Pair( 0, 1), // E
11+
new Pair(-1, 1), // SE
12+
new Pair(-1, 0), // S
13+
new Pair(-1, -1), // SW
14+
new Pair( 0, -1), // W
15+
new Pair( 1, -1)); // NW
16+
17+
/*
18+
* Search for multiple words in the given grid.
19+
*/
20+
Map<String, Optional<WordLocation>> search(final Set<String> words, final char[][] grid) {
21+
return words
22+
.stream()
23+
.collect(Collectors.toMap(Function.identity(), s -> search(s, grid)));
24+
}
25+
26+
/*
27+
* Search for a single word in the given grid.
28+
*/
29+
private Optional<WordLocation> search(final CharSequence word, final char[][] grid) {
30+
final int nRows = grid.length;
31+
final int nCols = grid[0].length;
32+
33+
// 1-indexed
34+
for (int c = 1; c <= nCols; c++) {
35+
for (int r = 1; r <= nRows; r++) {
36+
final Optional<WordLocation> wordLocation = search(word, grid, new Pair(c, r));
37+
if (wordLocation.isPresent()) return wordLocation;
38+
}
39+
}
40+
41+
return Optional.empty();
42+
}
43+
44+
/*
45+
* Search for a single word starting at a given coordinate within the given grid.
46+
*/
47+
private Optional<WordLocation> search(final CharSequence word, final char[][] grid, final Pair startCoord) {
48+
if (grid[startCoord.getY() - 1][startCoord.getX() - 1] != word.charAt(0)) return Optional.empty();
49+
50+
for (final Pair direction : DIRECTIONS) {
51+
final Optional<WordLocation> wordLocation = check(word, grid, startCoord, direction);
52+
if (wordLocation.isPresent()) return wordLocation;
53+
}
54+
55+
return Optional.empty();
56+
}
57+
58+
/*
59+
* Check whether a single word starts at a given coordinate and is aligned in a given direction within the given
60+
* grid.
61+
*/
62+
private Optional<WordLocation> check(
63+
final CharSequence word,
64+
final char[][] grid,
65+
final Pair startCoord,
66+
final Pair direction) {
67+
68+
final int nRows = grid.length;
69+
final int nCols = grid[0].length;
70+
71+
Pair nextCoord = startCoord;
72+
73+
for (int charIndex = 1; charIndex < word.length(); charIndex++) {
74+
nextCoord = new Pair(
75+
startCoord.getX() + charIndex * direction.getX(),
76+
startCoord.getY() + charIndex * direction.getY());
77+
78+
if (nextCoord.getY() < 1 ||
79+
nextCoord.getY() > nRows ||
80+
nextCoord.getX() < 1 ||
81+
nextCoord.getX() > nCols) {
82+
83+
return Optional.empty();
84+
}
85+
86+
if (grid[nextCoord.getY() - 1][nextCoord.getX() - 1] != word.charAt(charIndex)) {
87+
return Optional.empty();
88+
}
89+
}
90+
91+
return Optional.of(new WordLocation(startCoord, nextCoord));
92+
}
93+
94+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Pair {
2+
3+
private final int x;
4+
5+
private final int y;
6+
7+
Pair(final int x, final int y) {
8+
this.y = y;
9+
this.x = x;
10+
}
11+
12+
int getX() {
13+
return x;
14+
}
15+
16+
int getY() {
17+
return y;
18+
}
19+
20+
@Override
21+
public boolean equals(final Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
25+
Pair pair = (Pair) o;
26+
27+
return x == pair.x && y == pair.y;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
int result = x;
33+
result = 31 * result + y;
34+
return result;
35+
}
36+
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class WordLocation {
2+
3+
private final Pair startCoord;
4+
5+
private final Pair endCoord;
6+
7+
WordLocation(final Pair startCoord, final Pair endCoord) {
8+
this.startCoord = startCoord;
9+
this.endCoord = endCoord;
10+
}
11+
12+
@Override
13+
public boolean equals(final Object o) {
14+
if (this == o) return true;
15+
if (o == null || getClass() != o.getClass()) return false;
16+
17+
WordLocation that = (WordLocation) o;
18+
19+
return startCoord.equals(that.startCoord) && endCoord.equals(that.endCoord);
20+
}
21+
22+
@Override
23+
public int hashCode() {
24+
int result = startCoord.hashCode();
25+
result = 31 * result + endCoord.hashCode();
26+
return result;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)