Skip to content

Commit 1d27058

Browse files
committed
Trie
1 parent 855c4aa commit 1d27058

File tree

11 files changed

+249
-40
lines changed

11 files changed

+249
-40
lines changed

Java/Implement Trie (Prefix Tree).java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@
5353
TrieNode: contains the single character, and a list of children.
5454
Note: we will use hashmap<child character, child TrieNode>, because child access is O(1)
5555
*/
56-
5756
class Trie {
5857
class TrieNode {
5958
public boolean isEnd;
60-
public Map<Character, TrieNode> children;
59+
public Map<Character, TrieNode> children; // Map is more applicable to all chars, not limited to 256 ASCII
6160
public TrieNode() {
6261
this.isEnd = false;
6362
this.children = new HashMap<>();
@@ -72,33 +71,22 @@ public Trie() {
7271

7372
/** Inserts a word into the trie. */
7473
public void insert(String word) {
75-
if (word == null || word.length() == 0) {
76-
return;
77-
}
78-
if (search(word)) {
79-
return;
80-
}
74+
if (word == null || word.length() == 0 || search(word)) return;
75+
8176
TrieNode node = root;
82-
for (int i = 0; i < word.length(); i++) {
83-
char c = word.charAt(i);
84-
if (!node.children.containsKey(c)) {
85-
node.children.put(c, new TrieNode());
86-
}
77+
for (char c : word.toCharArray()) {
78+
node.children.putIfAbsent(c, new TrieNode());
8779
node = node.children.get(c);
88-
if (i == word.length() - 1) {
89-
node.isEnd = true;
90-
}
9180
}
81+
node.isEnd = true; // can set word to node as well, if needed
9282
}
9383

94-
/** Returns if the word is in the trie. */
84+
/** Returns if the word is in the trie: correct path + isEnd */
9585
public boolean search(String word) {
96-
if (word == null || word.length() == 0) {
97-
return false;
98-
}
86+
if (word == null || word.length() == 0) return false;
87+
9988
TrieNode node = root;
100-
for (int i = 0; i < word.length(); i++) {
101-
char c = word.charAt(i);
89+
for (char c : word.toCharArray()) {
10290
if (!node.children.containsKey(c)) {
10391
return false;
10492
}
@@ -109,12 +97,10 @@ public boolean search(String word) {
10997

11098
/** Returns if there is any word in the trie that starts with the given prefix. */
11199
public boolean startsWith(String prefix) {
112-
if (prefix == null || prefix.length() == 0) {
113-
return false;
114-
}
100+
if (prefix == null || prefix.length() == 0) return false;
101+
115102
TrieNode node = root;
116-
for (int i = 0; i < prefix.length(); i++) {
117-
char c = prefix.charAt(i);
103+
for (char c : prefix.toCharArray()) {
118104
if (!node.children.containsKey(c)) {
119105
return false;
120106
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
E
2+
1533408053
3+
tags: String
4+
5+
Read4 题目. 理解题目: 是有个input object buff, 会被populated with data.
6+
7+
#### String in char[] format
8+
- 理解题目: 其实就是track `可以read多少bytes by read4() response`
9+
- 另外一个有用的function `System.arraycopy(src, srcIndex, dest, destIndex, length)`
10+
11+
```
12+
/*
13+
The API: int read4(char *buf) reads 4 characters at a time from a file.
14+
15+
The return value is the actual number of characters read. For example,
16+
it returns 3 if there is only 3 characters left in the file.
17+
18+
By using the read4 API, implement the function int read(char *buf, int n)
19+
that reads n characters from the file.
20+
21+
Example 1:
22+
23+
Input: buf = "abc", n = 4
24+
Output: "abc"
25+
Explanation: The actual number of characters read is 3, which is "abc".
26+
Example 2:
27+
28+
Input: buf = "abcde", n = 5
29+
Output: "abcde"
30+
Note:
31+
The read function will only be called once for each test case.
32+
*/
33+
34+
/*
35+
- use temp to store all items read, and System.copy into buf.
36+
- track overall progress: i
37+
- if read4 returns < 4, mark end and return.
38+
*/
39+
public class Solution extends Reader4 {
40+
/**
41+
* @param buf Destination buffer
42+
* @param n Maximum number of characters to read
43+
* @return The number of characters read
44+
*/
45+
public int read(char[] buf, int n) {
46+
if (buf == null || n <= 0) return 0;
47+
int i = 0;
48+
while (i < n) {
49+
char[] temp = new char[4];
50+
int count = read4(temp);
51+
int range = i + 3 < n ? count : Math.min(n - i, count);
52+
System.arraycopy(temp, 0, buf, i, range);
53+
i += range;
54+
if (count < 4) break;
55+
}
56+
return i;
57+
}
58+
}
59+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
E
2+
1533407180
3+
tags: Tree
4+
5+
HashSet to store visited items. Same old 2 sum trick.
6+
7+
```
8+
/*
9+
Given a Binary Search Tree and a target number,
10+
return true if there exist two elements in the BST such that their sum is equal to the given target.
11+
12+
Example 1:
13+
Input:
14+
5
15+
/ \
16+
3 6
17+
/ \ \
18+
2 4 7
19+
20+
Target = 9
21+
22+
Output: True
23+
Example 2:
24+
Input:
25+
5
26+
/ \
27+
3 6
28+
/ \ \
29+
2 4 7
30+
31+
Target = 28
32+
33+
Output: False
34+
*/
35+
36+
// hashmap, in-order traverse for smaller items, recursively
37+
class Solution {
38+
Set<Integer> set = new HashSet<>();
39+
40+
public boolean findTarget(TreeNode root, int k) {
41+
if (root == null) return false;
42+
if (findTarget(root.left, k)) return true;
43+
if (set.contains(k - root.val)) return true;
44+
set.add(root.val);
45+
if (findTarget(root.right, k)) return true;
46+
47+
return false;
48+
}
49+
}
50+
```

LevelReviewPage.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33

4-
## Easy (146)
4+
## Easy (148)
55
**0. [Group Shifted Strings.java](https://github.com/awangdev/LintCode/blob/master/Java/Group%20Shifted%20Strings.java)** Level: Easy Tags: []
66

77
相同shift规则的string, 能被推算到同一个零起始点,就是共同减去一个char,最后就相等。以此作为key,用HashMap。一目了然。
@@ -2271,6 +2271,28 @@ space: O(1)
22712271

22722272
---
22732273

2274+
**146. [Two Sum IV - Input is a BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Two%20Sum%20IV%20-%20Input%20is%20a%20BST.java)** Level: Easy Tags: [Tree]
2275+
2276+
2277+
HashSet to store visited items. Same old 2 sum trick.
2278+
2279+
2280+
2281+
---
2282+
2283+
**147. [Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)** Level: Easy Tags: [String]
2284+
2285+
2286+
Read4 题目. 理解题目: 是有个input object buff, 会被populated with data.
2287+
2288+
#### String in char[] format
2289+
- 理解题目: 其实就是track `可以read多少bytes by read4() response`
2290+
- 另外一个有用的function `System.arraycopy(src, srcIndex, dest, destIndex, length)`
2291+
2292+
2293+
2294+
---
2295+
22742296

22752297

22762298

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,5 @@
485485
|464|[Sliding Window Median.java](https://github.com/awangdev/LintCode/blob/master/Java/Sliding%20Window%20Median.java)|Hard|Java|[Design, Heap, MaxHeap, MinHeap]||
486486
|465|[Inorder Successor in BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Inorder%20Successor%20in%20BST.java)|Medium|Java|[BST, Tree]||
487487
|466|[Subtree of Another Tree.java](https://github.com/awangdev/LintCode/blob/master/Java/Subtree%20of%20Another%20Tree.java)|Easy|Java|[DFS, Divide and Conquer, Tree]||
488+
|467|[Two Sum IV - Input is a BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Two%20Sum%20IV%20-%20Input%20is%20a%20BST.java)|Easy|Java|[Tree]||
489+
|468|[Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)|Easy|Java|[String]||

ReviewPage.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9229,5 +9229,27 @@ Data Stream Median 的同理题目: 不只是不断增加的Sequence, 而且要r
92299229

92309230

92319231

9232+
---
9233+
9234+
**467. [Two Sum IV - Input is a BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Two%20Sum%20IV%20-%20Input%20is%20a%20BST.java)** Level: Easy Tags: [Tree]
9235+
9236+
9237+
HashSet to store visited items. Same old 2 sum trick.
9238+
9239+
9240+
9241+
---
9242+
9243+
**468. [Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)** Level: Easy Tags: [String]
9244+
9245+
9246+
Read4 题目. 理解题目: 是有个input object buff, 会被populated with data.
9247+
9248+
#### String in char[] format
9249+
- 理解题目: 其实就是track `可以read多少bytes by read4() response`
9250+
- 另外一个有用的function `System.arraycopy(src, srcIndex, dest, destIndex, length)`
9251+
9252+
9253+
92329254
---
92339255

TagREADME.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Table of Contents
66
* [DFS (85)](#dfs-85)
77
* [DP (82)](#dp-82)
88
* [Hash Table (57)](#hash-table-57)
9-
* [Tree (51)](#tree-51)
10-
* [String (50)](#string-50)
9+
* [Tree (52)](#tree-52)
10+
* [String (51)](#string-51)
1111
* [Binary Search (39)](#binary-search-39)
1212
* [Math (34)](#math-34)
1313
* [Divide and Conquer (34)](#divide-and-conquer-34)
@@ -445,7 +445,7 @@ Table of Contents
445445

446446

447447

448-
## Tree (51)
448+
## Tree (52)
449449
| Squence | Problem | Level | Language | Tags | Video Tutorial|
450450
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
451451
|0|[Construct Binary Tree from Inorder and Preorder Traversal.java](https://github.com/awangdev/LintCode/blob/master/Java/Construct%20Binary%20Tree%20from%20Inorder%20and%20Preorder%20Traversal.java)|Medium|Java|[Array, DFS, Divide and Conquer, Hash Table, Tree]||
@@ -499,13 +499,14 @@ Table of Contents
499499
|48|[Convert Binary Search Tree to Sorted Doubly Linked List (extra space).java](https://github.com/awangdev/LintCode/blob/master/Java/Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List%20(extra%20space).java)|Medium|Java|[Linked List, Stack, Tree]||
500500
|49|[Maximum Binary Tree.java](https://github.com/awangdev/LintCode/blob/master/Java/Maximum%20Binary%20Tree.java)|Medium|Java|[Stack, Tree]||
501501
|50|[Binary Tree Postorder Traversal.java](https://github.com/awangdev/LintCode/blob/master/Java/Binary%20Tree%20Postorder%20Traversal.java)|Medium|Java|[Stack, Tree, Two Stacks]||
502+
|51|[Two Sum IV - Input is a BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Two%20Sum%20IV%20-%20Input%20is%20a%20BST.java)|Easy|Java|[Tree]||
502503

503504

504505

505506

506507

507508

508-
## String (50)
509+
## String (51)
509510
| Squence | Problem | Level | Language | Tags | Video Tutorial|
510511
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
511512
|0|[Unique Characters.java](https://github.com/awangdev/LintCode/blob/master/Java/Unique%20Characters.java)|Easy|Java|[Array, String]||
@@ -555,9 +556,10 @@ Table of Contents
555556
|44|[Reverse Words in a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Reverse%20Words%20in%20a%20String.java)|Medium|Java|[String]||
556557
|45|[Reverse Words in a String II.java](https://github.com/awangdev/LintCode/blob/master/Java/Reverse%20Words%20in%20a%20String%20II.java)|Medium|Java|[String]||
557558
|46|[Reverse Words in a String III.java](https://github.com/awangdev/LintCode/blob/master/Java/Reverse%20Words%20in%20a%20String%20III.java)|Easy|Java|[String]||
558-
|47|[Reverse Vowels of a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Reverse%20Vowels%20of%20a%20String.java)|Easy|Java|[String, Two Pointers]||
559-
|48|[Implement strStr().java](https://github.com/awangdev/LintCode/blob/master/Java/Implement%20strStr().java)|Easy|Java|[String, Two Pointers]||
560-
|49|[Valid Palindrome.java](https://github.com/awangdev/LintCode/blob/master/Java/Valid%20Palindrome.java)|Easy|Java|[String, Two Pointers]||
559+
|47|[Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)|Easy|Java|[String]||
560+
|48|[Reverse Vowels of a String.java](https://github.com/awangdev/LintCode/blob/master/Java/Reverse%20Vowels%20of%20a%20String.java)|Easy|Java|[String, Two Pointers]||
561+
|49|[Implement strStr().java](https://github.com/awangdev/LintCode/blob/master/Java/Implement%20strStr().java)|Easy|Java|[String, Two Pointers]||
562+
|50|[Valid Palindrome.java](https://github.com/awangdev/LintCode/blob/master/Java/Valid%20Palindrome.java)|Easy|Java|[String, Two Pointers]||
561563

562564

563565

TagReviewPage.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ SegmentTree大集合. Methods: `build, query, modify`. 不难。只是要都记
331331

332332

333333

334-
## String (50)
334+
## String (51)
335335
**0. [Space Replacement.java](https://github.com/awangdev/LintCode/blob/master/Java/Space%20Replacement.java)** Level: Medium Tags: [String]
336336

337337

@@ -1227,6 +1227,19 @@ time: O(n)
12271227

12281228
---
12291229

1230+
**50. [Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)** Level: Easy Tags: [String]
1231+
1232+
1233+
Read4 题目. 理解题目: 是有个input object buff, 会被populated with data.
1234+
1235+
#### String in char[] format
1236+
- 理解题目: 其实就是track `可以read多少bytes by read4() response`
1237+
- 另外一个有用的function `System.arraycopy(src, srcIndex, dest, destIndex, length)`
1238+
1239+
1240+
1241+
---
1242+
12301243

12311244

12321245

@@ -10218,7 +10231,7 @@ space: O(n)
1021810231

1021910232

1022010233

10221-
## Tree (51)
10234+
## Tree (52)
1022210235
**0. [Unique Binary Search Tree.java](https://github.com/awangdev/LintCode/blob/master/Java/Unique%20Binary%20Search%20Tree.java)** Level: Medium Tags: [BST, DP, Tree]
1022310236

1022410237

@@ -11288,6 +11301,15 @@ space: O(1)
1128811301

1128911302
---
1129011303

11304+
**51. [Two Sum IV - Input is a BST.java](https://github.com/awangdev/LintCode/blob/master/Java/Two%20Sum%20IV%20-%20Input%20is%20a%20BST.java)** Level: Easy Tags: [Tree]
11305+
11306+
11307+
HashSet to store visited items. Same old 2 sum trick.
11308+
11309+
11310+
11311+
---
11312+
1129111313

1129211314

1129311315

review/String.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33

4-
## String (50)
4+
## String (51)
55
**0. [Space Replacement.java](https://github.com/awangdev/LintCode/blob/master/Java/Space%20Replacement.java)** Level: Medium Tags: [String]
66

77

@@ -895,5 +895,18 @@ time: O(n)
895895

896896

897897

898+
---
899+
900+
**50. [Read N Characters Given Read4.java](https://github.com/awangdev/LintCode/blob/master/Java/Read%20N%20Characters%20Given%20Read4.java)** Level: Easy Tags: [String]
901+
902+
903+
Read4 题目. 理解题目: 是有个input object buff, 会被populated with data.
904+
905+
#### String in char[] format
906+
- 理解题目: 其实就是track `可以read多少bytes by read4() response`
907+
- 另外一个有用的function `System.arraycopy(src, srcIndex, dest, destIndex, length)`
908+
909+
910+
898911
---
899912

0 commit comments

Comments
 (0)