|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * = 79. Word Search |
| 7 | + * |
| 8 | + * https://leetcode.com/problems/word-search/[Word Search - LeetCode] |
| 9 | + * |
| 10 | + * Given a 2D board and a word, find if the word exists in the grid. |
| 11 | + * |
| 12 | + * The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. |
| 13 | + * |
| 14 | + * .Example: |
| 15 | + * [source] |
| 16 | + * ---- |
| 17 | + * board = |
| 18 | + * [ |
| 19 | + * ['A','B','C','E'], |
| 20 | + * ['S','F','C','S'], |
| 21 | + * ['A','D','E','E'] |
| 22 | + * ] |
| 23 | + * |
| 24 | + * Given word = "ABCCED", return true. |
| 25 | + * Given word = "SEE", return true. |
| 26 | + * Given word = "ABCB", return false. |
| 27 | + * ---- |
| 28 | + * |
| 29 | + * @author D瓜哥, https://www.diguage.com/ |
| 30 | + * @since 2020-01-03 17:31 |
| 31 | + */ |
| 32 | +public class WordSearch { |
| 33 | + |
| 34 | + /** |
| 35 | + * Runtime: 4 ms, faster than 89.90% of Java online submissions for Word Search. |
| 36 | + * |
| 37 | + * Memory Usage: 38.4 MB, less than 97.96% of Java online submissions for Word Search. |
| 38 | + * |
| 39 | + * Copy from: https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space.[Accepted very short Java solution. No additional space. - LeetCode Discuss] |
| 40 | + */ |
| 41 | + public boolean exist(char[][] board, String word) { |
| 42 | + if (Objects.isNull(board) || board.length == 0) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + if (Objects.isNull(word) || word.length() == 0) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + char[] chars = word.toCharArray(); |
| 49 | + for (int y = 0; y < board.length; y++) { |
| 50 | + for (int x = 0; x < board[y].length; x++) { |
| 51 | + if (exist(board, y, x, chars, 0)) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + private boolean exist(char[][] board, int y, int x, char[] word, int i) { |
| 60 | + if (i == word.length) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + if (y < 0 || x < 0 || y == board.length || x == board[y].length) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + if (board[y][x] != word[i]) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + board[y][x] = Character.MAX_VALUE; |
| 70 | + boolean existable = exist(board, y - 1, x, word, i + 1) |
| 71 | + || exist(board, y, x + 1, word, i + 1) |
| 72 | + || exist(board, y + 1, x, word, i + 1) |
| 73 | + || exist(board, y, x - 1, word, i + 1); |
| 74 | + board[y][x] = word[i]; |
| 75 | + return existable; |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String[] args) { |
| 79 | + WordSearch solution = new WordSearch(); |
| 80 | + char[][] board = { |
| 81 | + {'A', 'B', 'C', 'E'}, |
| 82 | + {'S', 'F', 'C', 'S'}, |
| 83 | + {'A', 'D', 'E', 'E'}}; |
| 84 | + String w1 = "ABCCED"; |
| 85 | + boolean r1 = solution.exist(board, w1); |
| 86 | + System.out.println((r1) + " : " + r1); |
| 87 | + |
| 88 | + String w2 = "SEE"; |
| 89 | + boolean r2 = solution.exist(board, w2); |
| 90 | + System.out.println((r2) + " : " + r2); |
| 91 | + |
| 92 | + String w3 = "ABCB"; |
| 93 | + boolean r3 = solution.exist(board, w3); |
| 94 | + System.out.println((!r3) + " : " + r3); |
| 95 | + } |
| 96 | +} |
0 commit comments