Skip to content

Commit 6e3f9c0

Browse files
committed
init commit
0 parents  commit 6e3f9c0

File tree

17 files changed

+820
-0
lines changed

17 files changed

+820
-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: 143 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: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# java_implement_trie
2+
3+
**[trie](https://en.wikipedia.org/wiki/Trie)** (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
4+
5+
Implement the Trie class:
6+
7+
- `Trie()` Initializes the trie object.
8+
- `void insert(String word)` Inserts the string `word` into the trie.
9+
- `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise.
10+
- `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise.
11+
12+
## Examples
13+
14+
**Example 1:**
15+
16+
```
17+
Input
18+
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
19+
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
20+
Output
21+
[null, null, true, false, true, null, true]
22+
23+
Explanation
24+
Trie trie = new Trie();
25+
trie.insert("apple");
26+
trie.search("apple"); // return True
27+
trie.search("app"); // return False
28+
trie.startsWith("app"); // return True
29+
trie.insert("app");
30+
trie.search("app"); // return True
31+
32+
```
33+
34+
**Constraints:**
35+
36+
- `1 <= word.length, prefix.length <= 2000`
37+
- `word` and `prefix` consist only of lowercase English letters.
38+
- At most $`3*10^4`$ calls **in total** will be made to `insert``search`, and `startsWith`.
39+
40+
## 解析
41+
42+
題目要我們實作一個可以儲存所有小寫英文字的一個 Trie結構
43+
44+
需要實作出以下 method:
45+
46+
1. Constructor: 用來來建構 Trie
47+
2. insert(word string): 用來儲存 word
48+
3. search(word string) bool: 用來察看是否有儲存過這個 word
49+
4. startsWith(prefix string) bool: 用來察看是否有儲存過包含 prefix 的 word
50+
51+
首先知道 Trie 是一種特殊的 Tree
52+
53+
目前需要儲存所有字元是小寫英文 a-z 組成的字串
54+
55+
為了節省空間所以會是已字元為單位來做存儲
56+
57+
另外是可以透過把 字元當成每個 node 的 edge 上 key
58+
59+
如下圖:
60+
61+
![](https://i.imgur.com/JTyTsFg.png)
62+
63+
每個結點都可以透過 key 來找到下一個結點
64+
65+
這個結構可以使用 hashmap 來實作
66+
67+
而沒每個結點除了這個用來紀錄 child 對應的 hashmap 外
68+
69+
需要一個布林值來紀錄這個結點是否為最後一個結點
70+
71+
如上圖的 Trie 雖然有 appl 的 prefix 但是卻沒有 appl 這個字
72+
73+
## 程式碼
74+
75+
```java
76+
public class Trie {
77+
static class TrieNode {
78+
boolean EndOfWord = false;
79+
HashMap<Character, TrieNode> Children = new HashMap<>();
80+
}
81+
TrieNode root;
82+
public Trie() {
83+
root = new TrieNode();
84+
}
85+
public void insert(String word) {
86+
TrieNode cur = root;
87+
int size = word.length();
88+
for (int pos = 0; pos < size; pos++) {
89+
char ch = word.charAt(pos);
90+
if (!cur.Children.containsKey(ch)) {
91+
cur.Children.put(ch, new TrieNode());
92+
}
93+
cur = cur.Children.get(ch);
94+
}
95+
cur.EndOfWord = true;
96+
}
97+
public boolean search(String word) {
98+
TrieNode cur = root;
99+
int size = word.length();
100+
for (int pos = 0; pos < size; pos++) {
101+
char ch = word.charAt(pos);
102+
if (!cur.Children.containsKey(ch)) {
103+
return false;
104+
}
105+
cur = cur.Children.get(ch);
106+
}
107+
return cur.EndOfWord;
108+
}
109+
public boolean startsWith(String prefix) {
110+
TrieNode cur = root;
111+
int size = prefix.length();
112+
for (int pos = 0; pos < size; pos++) {
113+
char ch = prefix.charAt(pos);
114+
if (!cur.Children.containsKey(ch)) {
115+
return false;
116+
}
117+
cur = cur.Children.get(ch);
118+
}
119+
return true;
120+
}
121+
}
122+
```
123+
124+
## 困難點
125+
126+
1. 轉換紀錄 edge的值當作 index
127+
128+
## Solve Point
129+
130+
- [x] Understand What problem need to solve
131+
- [x] Analysis complexity

0 commit comments

Comments
 (0)