-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path압축.js
More file actions
30 lines (24 loc) · 626 Bytes
/
압축.js
File metadata and controls
30 lines (24 loc) · 626 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
const dict = new Map(
Array.from({ length: 26 }).map((_, index) => [String.fromCharCode(65 + index), index + 1]),
);
const findLongestMatch = (target, startIndex) => {
let match = '';
for (let i = startIndex; i < target.length; i += 1) {
if (dict.has(match + target[i])) match += target[i];
else break;
}
return match;
};
function solution(msg) {
const sol = [];
let i = 0;
while (i < msg.length) {
const match = findLongestMatch(msg, i);
sol.push(dict.get(match));
i += match.length;
if (i < msg.length) {
dict.set(match + msg[i], dict.size + 1);
}
}
return sol;
}