Repository navigation
Expand file tree
/
Copy pathWordSearch.java
More file actions
84 lines (80 loc) · 2.31 KB
/
Copy pathWordSearch.java
File metadata and controls
84 lines (80 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Solution {
// Given an m x n board of characters and a word, return true if the word exists
// in the grid. The word can be constructed from letters in adjacent cells
// (horizontally or vertically) but cannot reuse the same letter cell more than
// once.
//
// Input:
// board = [
// ["A","B","C","E"],
// ["S","F","C","S"],
// ["A","D","E","E"]
// ]
// word = "ABCCED"
// Output: true
//
// Input:
// board = [
// ["A","B","C","E"],
// ["S","F","C","S"],
// ["A","D","E","E"]
// ]
// word = "SEE"
// Output: true
//
// Input:
// board = [
// ["A","B","C","E"],
// ["S","F","C","S"],
// ["A","D","E","E"]
// ]
// word = "ABCB"
// Output: false
public static boolean dfs(int r, int c, int m, int n, List<List<String>> board, String word, int index) {
if (index == word.length())
return true;
if (r < 0 || c < 0 || r >= m || c >= n || board.get(r).get(c) == "#"
|| board.get(r).get(c).charAt(0) != word.charAt(index)) {
return false;
}
String temp = board.get(r).get(c);
board.get(r).set(c, "#");
boolean found = dfs(r + 1, c, m, n, board, word, index + 1)
|| dfs(r, c + 1, m, n, board, word, index + 1)
|| dfs(r - 1, c, m, n, board, word, index + 1)
|| dfs(r, c - 1, m, n, board, word, index + 1);
board.get(r).set(c, temp);
return found;
}
public static boolean exist(List<List<String>> board, String word) {
int m = board.size();
int n = board.get(0).size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfs(i, j, m, n, board, word, 0)) {
return true;
}
}
}
return false;
}
public static List<String> splitWords(String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int boardLength = Integer.parseInt(scanner.nextLine());
List<List<String>> board = new ArrayList<>();
for (int i = 0; i < boardLength; i++) {
board.add(splitWords(scanner.nextLine()));
}
String word = scanner.nextLine();
scanner.close();
boolean res = exist(board, word);
System.out.println(res);
}
}