Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b6dc68

Browse files
committed
feat: 압축-카카오 문제 reduce 활용 풀이 추가
1 parent 9c743c6 commit 0b6dc68

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

‎level-2/[3차]-압축&17684&.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,52 @@ function solution(msg) {
3131
}
3232
return outputs;
3333
}
34+
35+
// 정답 2 - ssi02014
36+
function solution(msg) {
37+
const result = [];
38+
const dict = [
39+
"A",
40+
"B",
41+
"C",
42+
"D",
43+
"E",
44+
"F",
45+
"G",
46+
"H",
47+
"I",
48+
"J",
49+
"K",
50+
"L",
51+
"M",
52+
"N",
53+
"O",
54+
"P",
55+
"Q",
56+
"R",
57+
"S",
58+
"T",
59+
"U",
60+
"V",
61+
"W",
62+
"X",
63+
"Y",
64+
"Z",
65+
];
66+
67+
// 시간 복잡도 O(N^2)
68+
const lastWordAndCompression = msg.split("").reduce((acc, cur) => {
69+
const nextWord = acc + cur;
70+
const nextWordIdx = dict.indexOf(nextWord);
71+
const prevWordIdx = dict.indexOf(acc);
72+
73+
if (nextWordIdx !== -1) return acc + cur;
74+
dict.push(nextWord);
75+
76+
if (prevWordIdx !== -1) result.push(prevWordIdx + 1);
77+
return cur;
78+
}, "");
79+
80+
result.push(dict.indexOf(lastWordAndCompression) + 1);
81+
return result;
82+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /