Skip to content

Commit 48d17b0

Browse files
add 1804
1 parent d416d34 commit 48d17b0

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ _If you like this project, please leave me a star._ ★
8484
|1807|[Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1807.java) ||Medium|HashTable, String|
8585
|1806|[Minimum Number of Operations to Reinitialize a Permutation](https://leetcode.com/problems/minimum-number-of-operations-to-reinitialize-a-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1806.java) ||Medium|Array, Greedy|
8686
|1805|[Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1805.java) ||Medium|String|
87+
|1804|[Implement Trie II (Prefix Tree)](https://leetcode.com/problems/implement-trie-ii-prefix-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1804.java) ||Medium|Trie, Design|
8788
|1800|[Maximum Ascending Subarray Sum](https://leetcode.com/problems/maximum-ascending-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1800.java) ||Easy|Two Pointers|
8889
|1797|[Design Authentication Manager](https://leetcode.com/problems/design-authentication-manager/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1797.java) ||Medium|HashTable, Design|
8990
|1796|[Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1796.java) ||Easy|String|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1804 {
4+
public static class Solution1 {
5+
public static class Trie {
6+
class TrieNode {
7+
int count;
8+
int wordsCount;
9+
TrieNode[] children;
10+
11+
public TrieNode() {
12+
this.children = new TrieNode[26];
13+
}
14+
15+
boolean isWord;
16+
}
17+
18+
TrieNode root;
19+
20+
public Trie() {
21+
root = new TrieNode();
22+
}
23+
24+
public void insert(String word) {
25+
TrieNode node = this.root;
26+
for (char c : word.toCharArray()) {
27+
if (node.children[c - 'a'] == null) {
28+
node.children[c - 'a'] = new TrieNode();
29+
}
30+
if (node.children[c - 'a'].count < 0) {
31+
node.children[c - 'a'].count = 0;
32+
}
33+
node.children[c - 'a'].count++;
34+
node = node.children[c - 'a'];
35+
}
36+
node.isWord = true;
37+
if (node.wordsCount < 0) {
38+
node.wordsCount = 0;
39+
}
40+
node.wordsCount++;
41+
}
42+
43+
public int countWordsEqualTo(String word) {
44+
TrieNode node = this.root;
45+
for (char c : word.toCharArray()) {
46+
if (node.children[c - 'a'] == null) {
47+
return 0;
48+
}
49+
node = node.children[c - 'a'];
50+
}
51+
return node.isWord ? node.wordsCount : 0;
52+
}
53+
54+
public int countWordsStartingWith(String prefix) {
55+
TrieNode node = this.root;
56+
for (char c : prefix.toCharArray()) {
57+
if (node.children[c - 'a'] == null) {
58+
return 0;
59+
}
60+
node = node.children[c - 'a'];
61+
}
62+
return node.count < 0 ? 0 : node.count;
63+
}
64+
65+
public void erase(String word) {
66+
TrieNode node = this.root;
67+
for (char c : word.toCharArray()) {
68+
node.children[c - 'a'].count--;
69+
node = node.children[c - 'a'];
70+
}
71+
node.wordsCount--;
72+
}
73+
}
74+
}
75+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1804;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1804Test {
10+
private static _1804.Solution1.Trie solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
15+
}
16+
17+
@Test
18+
public void test1() {
19+
solution1 = new _1804.Solution1.Trie();
20+
solution1.insert("apple");
21+
solution1.insert("apple");
22+
assertEquals(2, solution1.countWordsEqualTo("apple"));
23+
assertEquals(2, solution1.countWordsStartingWith("app"));
24+
solution1.erase("apple");
25+
assertEquals(1, solution1.countWordsEqualTo("apple"));
26+
assertEquals(1, solution1.countWordsStartingWith("app"));
27+
solution1.erase("apple");
28+
assertEquals(0, solution1.countWordsStartingWith("app"));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)