Skip to content

Commit 266dde4

Browse files
authored
Merge pull request #1332 from AlgorithmWithGod/zinnnn37
[20251106] PGM / LV2 / 전화번호 목록 / 김민진
2 parents b4f2d46 + 5b13ce6 commit 266dde4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
```java
2+
class PGM_LV2_전화번호_목록 {
3+
4+
private static Trie trie;
5+
private static boolean answer;
6+
7+
private static class TrieNode {
8+
TrieNode[] children;
9+
boolean isEnd;
10+
11+
TrieNode() {
12+
children = new TrieNode[10];
13+
isEnd = false;
14+
}
15+
}
16+
17+
private static class Trie {
18+
TrieNode root;
19+
20+
Trie() {
21+
root = new TrieNode();
22+
}
23+
24+
public static boolean insert(String number) {
25+
TrieNode cur = trie.root;
26+
27+
int len = number.length();
28+
for (int i = 0; i < len; i++) {
29+
int idx = number.charAt(i) - '0';
30+
31+
if (cur.children[idx] == null) {
32+
cur.children[idx] = new TrieNode();
33+
}
34+
35+
cur = cur.children[idx];
36+
37+
if (cur.isEnd) {
38+
return false;
39+
}
40+
}
41+
42+
for (int i = 0; i < 10; i++) {
43+
if (cur.children[i] != null) {
44+
return false;
45+
}
46+
}
47+
cur.isEnd = true;
48+
return true;
49+
}
50+
}
51+
52+
public boolean solution(String[] phone_book) {
53+
trie = new Trie();
54+
55+
for (String number : phone_book) {
56+
if (!trie.insert(number)) {
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
}
63+
```

0 commit comments

Comments
 (0)