forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
69 lines (65 loc) · 1.82 KB
/
Solution.java
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
class Solution {
private int[] counter;
private char[][] board;
public List<String> findWords(char[][] board, String[] words) {
counter = new int[26];
this.board = board;
for (char[] b : board) {
for (char c : b) {
++counter[c - 'a'];
}
}
Set<String> s = new HashSet<>(Arrays.asList(words));
List<String> ans = new ArrayList<>();
for (String word : s) {
if (find(word)) {
ans.add(word);
}
}
return ans;
}
private boolean find(String word) {
if (!check(word)) {
return false;
}
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
if (dfs(i, j, 0, word)) {
return true;
}
}
}
return false;
}
private boolean dfs(int i, int j, int l, String word) {
if (l == word.length()) {
return true;
}
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(l)) {
return false;
}
char c = board[i][j];
board[i][j] = '0';
boolean ans = false;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
ans = ans || dfs(x, y, l + 1, word);
}
board[i][j] = c;
return ans;
}
private boolean check(String word) {
int[] cnt = new int[26];
for (char c : word.toCharArray()) {
++cnt[c - 'a'];
}
for (int i = 0; i < 26; ++i) {
if (counter[i] < cnt[i]) {
return false;
}
}
return true;
}
}