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

Commitdcc11e6

Browse files
authored
Update Stream of Characters.java
1 parentb659134 commitdcc11e6

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

‎Hard/Stream of Characters.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
classStreamChecker {
2-
TrieNoderoot;
2+
33
Deque<Character>stream;
4+
TrieNoderoot;
45
publicStreamChecker(String[]words) {
56
root =newTrieNode('-');
6-
stream =newArrayDeque();
7-
for (Stringword :words) {
8-
TrieNodecurr =root;
9-
for (inti =word.length() -1;i >=0;i--) {
10-
if (!curr.map.containsKey(word.charAt(i))) {
11-
curr.map.put(word.charAt(i),newTrieNode(word.charAt(i)));
12-
}
13-
curr =curr.map.get(word.charAt(i));
7+
stream =newArrayDeque<>();
8+
Arrays.stream(words).forEach(word ->addWord(word));
9+
}
10+
11+
publicvoidaddWord(Stringword) {
12+
TrieNodecurr =root;
13+
for (inti =word.length() -1;i >=0;i--) {
14+
charc =word.charAt(i);
15+
if (curr.children[c -'a'] ==null) {
16+
curr.children[c -'a'] =newTrieNode(c);
1417
}
15-
curr.isWord =true;
18+
curr =curr.children[c -'a'];
1619
}
20+
curr.isWord =true;
1721
}
1822

1923
publicbooleanquery(charletter) {
@@ -23,25 +27,24 @@ public boolean query(char letter) {
2327
if (curr.isWord) {
2428
returntrue;
2529
}
26-
if (!curr.map.containsKey(c)) {
30+
if (curr.children[c -'a'] ==null) {
2731
returnfalse;
2832
}
29-
curr =curr.map.get(c);
33+
curr =curr.children[c -'a'];
3034
}
3135
returncurr.isWord;
3236
}
33-
}
34-
35-
36-
classTrieNode {
37-
charc;
38-
Map<Character,TrieNode>map;
39-
booleanisWord;
4037

41-
publicTrieNode(charc) {
42-
this.c =c;
43-
map =newHashMap<>();
44-
isWord =false;
38+
39+
classTrieNode {
40+
charc;
41+
TrieNode[]children;
42+
booleanisWord;
43+
44+
publicTrieNode(charc) {
45+
this.c =c;
46+
children =newTrieNode[26];
47+
}
4548
}
4649
}
4750

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp