Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48d17b0

Browse files
add 1804
1 parentd416d34 commit48d17b0

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+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1804 {
4+
publicstaticclassSolution1 {
5+
publicstaticclassTrie {
6+
classTrieNode {
7+
intcount;
8+
intwordsCount;
9+
TrieNode[]children;
10+
11+
publicTrieNode() {
12+
this.children =newTrieNode[26];
13+
}
14+
15+
booleanisWord;
16+
}
17+
18+
TrieNoderoot;
19+
20+
publicTrie() {
21+
root =newTrieNode();
22+
}
23+
24+
publicvoidinsert(Stringword) {
25+
TrieNodenode =this.root;
26+
for (charc :word.toCharArray()) {
27+
if (node.children[c -'a'] ==null) {
28+
node.children[c -'a'] =newTrieNode();
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+
publicintcountWordsEqualTo(Stringword) {
44+
TrieNodenode =this.root;
45+
for (charc :word.toCharArray()) {
46+
if (node.children[c -'a'] ==null) {
47+
return0;
48+
}
49+
node =node.children[c -'a'];
50+
}
51+
returnnode.isWord ?node.wordsCount :0;
52+
}
53+
54+
publicintcountWordsStartingWith(Stringprefix) {
55+
TrieNodenode =this.root;
56+
for (charc :prefix.toCharArray()) {
57+
if (node.children[c -'a'] ==null) {
58+
return0;
59+
}
60+
node =node.children[c -'a'];
61+
}
62+
returnnode.count <0 ?0 :node.count;
63+
}
64+
65+
publicvoiderase(Stringword) {
66+
TrieNodenode =this.root;
67+
for (charc :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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1804;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1804Test {
10+
privatestatic_1804.Solution1.Triesolution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp