Skip to content

Commit 1eb1333

Browse files
committed
Feat: 6주차 문제
1 parent 328b43f commit 1eb1333

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

Diff for: 6주차/박종운/레벨2__전화번호_목록.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class Node {
2+
constructor(key = "", value = null) {
3+
this.key = key;
4+
this.value = value;
5+
this.children = new Map();
6+
}
7+
}
8+
class Trie {
9+
constructor() {
10+
this.root = new Node();
11+
}
12+
insert(text) {
13+
let currentNode = this.root;
14+
for (let i = 0; i < text.length; i++) {
15+
const char = text[i];
16+
if (!currentNode.children.has(char)) {
17+
const key = currentNode.key + char;
18+
const isLastWordIndex = i === text.length - 1;
19+
const value = isLastWordIndex ? currentNode.key + char : null;
20+
currentNode.children.set(char, new Node(key, value));
21+
}
22+
currentNode = currentNode.children.get(char);
23+
}
24+
}
25+
has(text) {
26+
let currentNode = this.root;
27+
for (const char of text) {
28+
currentNode = currentNode.children.get(char);
29+
if (currentNode === false) {
30+
return false;
31+
}
32+
}
33+
return currentNode.value === text;
34+
}
35+
autoComplete(text) {
36+
let currentNode = this.root;
37+
const children = [];
38+
39+
for (const char of text) {
40+
currentNode = currentNode.children.get(char);
41+
if (currentNode === false) {
42+
console.log("not found node!");
43+
return [];
44+
}
45+
}
46+
47+
function recursion(node) {
48+
if (node.value) {
49+
children.push(node.value);
50+
}
51+
if (node.children.size) {
52+
node.children.forEach((el) => {
53+
recursion(el);
54+
});
55+
}
56+
}
57+
recursion(currentNode);
58+
return children;
59+
}
60+
}
61+
62+
function solution(phone_book) {
63+
var answer = true;
64+
65+
const trie = new Trie();
66+
67+
phone_book.sort((a,b) => b.length - a.length);
68+
69+
for(i=0; i<phone_book.length; i++) {
70+
const num = phone_book[i]
71+
72+
trie.insert(num);
73+
74+
const temp = trie.autoComplete(num);
75+
76+
if(temp.length && temp[0] !== num) {
77+
answer = false;
78+
break;
79+
}
80+
}
81+
82+
return answer
83+
}

Diff for: 6주차/박종운/실버1__접두사_찾기.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const local_input = `
2+
5 10
3+
baekjoononlinejudge
4+
startlink
5+
codeplus
6+
sundaycoding
7+
codingsh
8+
baekjoon
9+
star
10+
start
11+
code
12+
sunday
13+
coding
14+
cod
15+
online
16+
judge
17+
plus
18+
`;
19+
20+
const input = process.execArgv.includes("--stack-size=65536")
21+
? require("fs").readFileSync("dev/stdin").toString()
22+
: local_input;
23+
24+
const lines = input.trim().split("\n");
25+
const [N] = lines[0].split(' ').map(Number);
26+
const strs = lines.slice(1, N+1);
27+
const prefixs = lines.slice(N+1);
28+
29+
const set = new Set();
30+
31+
strs.forEach(str => {
32+
for(i=1; i<=str.length; i++) {
33+
set.add(str.slice(0, i));
34+
}
35+
});
36+
37+
let count = 0;
38+
39+
prefixs.forEach(prefix => {
40+
set.has(prefix) && count++
41+
})
42+
43+
console.log(count)

0 commit comments

Comments
 (0)