File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments