-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1141.js
More file actions
48 lines (35 loc) · 781 Bytes
/
1141.js
File metadata and controls
48 lines (35 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
h
| \
i ello
r
| \
un erun
|
ning
이런식으로 트리를 구성
이파리 개수를 센다.
*/
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [_, ...words] = require('fs').readFileSync(INPUT_FILE).toString().trim()
.split('\n');
const tree = {};
const makeTree = (word) => {
let root = tree;
for (let i = 0; i < word.length; i += 1) {
const char = word[i];
if (!root[char]) root[char] = {};
root = root[char];
}
};
words.forEach((word) => makeTree(word));
let leafCount = 0;
const dfs = (root) => {
if (Object.keys(root).length === 0) {
leafCount += 1;
return;
}
Object.values(root).forEach((child) => dfs(child));
};
Object.values(tree).forEach((root) => dfs(root));
console.log(leafCount);