|
| 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 | +} |
0 commit comments