Skip to content

Commit b1abdeb

Browse files
Merge pull request #721 from eric-hjh/main
[황장현] 113차 라이브 코테 제출
2 parents 7f683a6 + 13d8077 commit b1abdeb

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n');
6+
7+
class TrieNode {
8+
constructor() {
9+
this.children = {};
10+
this.isEnd = false;
11+
}
12+
}
13+
14+
function isConsistent(phoneNumbers) {
15+
const root = new TrieNode();
16+
17+
for (const number of phoneNumbers) {
18+
let node = root;
19+
20+
for (let i = 0; i < number.length; i++) {
21+
const char = number[i];
22+
23+
if (!node.children[char]) {
24+
node.children[char] = new TrieNode();
25+
}
26+
27+
node = node.children[char];
28+
29+
if (node.isEnd) {
30+
return false;
31+
}
32+
}
33+
if (Object.keys(node.children).length > 0) {
34+
return false;
35+
}
36+
node.isEnd = true;
37+
}
38+
39+
return true;
40+
}
41+
42+
function solution(input) {
43+
const t = parseInt(input[0]);
44+
let index = 1;
45+
46+
for (let i = 0; i < t; i++) {
47+
const n = parseInt(input[index]);
48+
const phoneNumbers = input.slice(index + 1, index + 1 + n);
49+
isConsistent(phoneNumbers) ? console.log('YES') : console.log('NO');
50+
51+
index += n + 1;
52+
}
53+
}
54+
55+
solution(input);

0 commit comments

Comments
 (0)