Skip to content

Commit d3b9195

Browse files
committed
init commit
0 parents  commit d3b9195

File tree

16 files changed

+845
-0
lines changed

16 files changed

+845
-0
lines changed

.github/workflows/java.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Java
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Setup JDK 11
18+
uses: actions/setup-java@v3
19+
with:
20+
distribution: 'adopt'
21+
java-version: '11'
22+
- name: Grant execution permission for gradlew
23+
run: chmod +x gradlew
24+
- name: Cache Gradle packages
25+
uses: actions/cache@v3
26+
with:
27+
path: ~/.gradle/caches
28+
key: ${{ runner.os }}-gradle-${{ github.event.repository.name }}-${{ hashFiles('**/*.gradle')}}
29+
restore-keys: ${{ runner.os }}-gradle-${{ github.event.repository.name }}
30+
- name: Build with Gradlew
31+
run: ./gradlew build
32+
- name: Test with Gradlew
33+
run: ./gradlew :test --tests "SolutionTest"

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
./gradlew :test --tests "SolutionTest"

.idea/gradle.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# java_word_search_v2
2+
3+
Given an `m x n` `board` of characters and a list of strings `words`, return *all words on the board*.
4+
5+
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
6+
7+
## Examples
8+
9+
**Example 1:**
10+
11+
![https://assets.leetcode.com/uploads/2020/11/07/search1.jpg](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg)
12+
13+
```
14+
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
15+
Output: ["eat","oath"]
16+
17+
```
18+
19+
**Example 2:**
20+
21+
![https://assets.leetcode.com/uploads/2020/11/07/search2.jpg](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg)
22+
23+
```
24+
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
25+
Output: []
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `m == board.length`
32+
- `n == board[i].length`
33+
- `1 <= m, n <= 12`
34+
- `board[i][j]` is a lowercase English letter.
35+
- `1 <= words.length <= 3*10^4`
36+
- `1 <= words[i].length <= 10`
37+
- `words[i]` consists of lowercase English letters.
38+
- All the strings of `words` are unique.
39+
40+
## 解析
41+
42+
題目給定一個 m by n 字元矩陣 board 還有一個字串 array words
43+
44+
要求實作一個演算法找出字串 array words 有哪些字串存在於 m by n 矩陣
45+
46+
在 m by n 矩陣搜詢一個字串做的法是從每個字元當作起點針對上下左右四個方向做 DFS 找尋有可能的字串,所以最遭的狀況就是 $(4^m)^n$
47+
48+
Trie 的結構能夠有效讓要搜尋的字串可以用一個很有效率的方式做比對
49+
50+
如下圖
51+
52+
![](https://i.imgur.com/rrf2Mmj.png)
53+
54+
## 程式碼
55+
```java
56+
import java.util.ArrayList;
57+
import java.util.HashMap;
58+
import java.util.HashSet;
59+
import java.util.List;
60+
61+
public class Solution {
62+
static class Node {
63+
HashMap<Character, Node> Children = new HashMap<>();
64+
boolean isWord = false;
65+
String Word = "";
66+
}
67+
static class Trie {
68+
Node root;
69+
Trie() {
70+
root = new Node();
71+
}
72+
public void addWord(String word) {
73+
Node cur = root;
74+
int size = word.length();
75+
for (int pos = 0; pos < size; pos++) {
76+
char ch = word.charAt(pos);
77+
if (!cur.Children.containsKey(ch)) {
78+
cur.Children.put(ch, new Node());
79+
}
80+
cur = cur.Children.get(ch);
81+
}
82+
cur.isWord = true;
83+
cur.Word = word;
84+
}
85+
}
86+
int ROW, COL;
87+
public List<String> findWords(char[][] board, String[] words) {
88+
ROW = board.length;
89+
COL = board[0].length;
90+
boolean[][] visit = new boolean[ROW][COL];
91+
HashSet<String> hashSet = new HashSet<>();
92+
Trie trie = new Trie();
93+
for (String word : words) {
94+
trie.addWord(word);
95+
}
96+
for (int row = 0; row < ROW; row++) {
97+
for (int col = 0; col < COL; col++) {
98+
DFS(row, col, trie.root, visit, board, hashSet, words.length);
99+
}
100+
}
101+
return new ArrayList<>(hashSet);
102+
}
103+
public void DFS(int row, int col, Node node, boolean[][] visit,
104+
char[][] board, HashSet<String> hashSet, int max) {
105+
Node cur = node;
106+
if (row < 0 || row >= ROW || col < 0 || col >= COL) {
107+
return;
108+
}
109+
if (visit[row][col]) {
110+
return;
111+
}
112+
char ch = board[row][col];
113+
if (!cur.Children.containsKey(ch)) {
114+
return;
115+
}
116+
visit[row][col] = true;
117+
cur = cur.Children.get(ch);
118+
if (cur.isWord) {
119+
hashSet.add(cur.Word);
120+
}
121+
if (hashSet.size() == max) {
122+
return;
123+
}
124+
DFS(row-1, col, cur, visit, board, hashSet, max);
125+
DFS(row+1, col, cur, visit, board, hashSet, max);
126+
DFS(row, col-1, cur, visit, board, hashSet, max);
127+
DFS(row, col+1, cur, visit, board, hashSet, max);
128+
visit[row][col] = false;
129+
}
130+
}
131+
132+
```
133+
## 困難點
134+
135+
1. 理解使用 DFS 針對 4個方向去找可能的值
136+
2. 理解透過 Tries 來減少搜尋的次數,透過 Trie 結構可以一次找到多個 prefix 一樣的字串
137+
138+
## Solve Point
139+
140+
- [x] Understand what problem need to solve
141+
- [x] Analysis complexity

0 commit comments

Comments
 (0)