Skip to content

Commit bf55435

Browse files
Merge pull request #102 from ssi02014/ssi02014
feat: 압축-카카오 문제 reduce 활용 풀이 추가
2 parents 9c743c6 + 63d4698 commit bf55435

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,25 @@ function solution(msg) {
3131
}
3232
return outputs;
3333
}
34+
35+
// 정답 2 - ssi02014
36+
function solution(msg) {
37+
const result = [];
38+
const dict = Array.from({length: 26},(_, i) => String.fromCharCode(65 + i))
39+
40+
// 시간 복잡도 O(N^2)
41+
const lastWordAndCompression = msg.split("").reduce((acc, cur) => {
42+
const nextWord = acc + cur;
43+
const nextWordIdx = dict.indexOf(nextWord);
44+
const prevWordIdx = dict.indexOf(acc);
45+
46+
if (nextWordIdx !== -1) return acc + cur;
47+
dict.push(nextWord);
48+
49+
if (prevWordIdx !== -1) result.push(prevWordIdx + 1);
50+
return cur;
51+
}, "");
52+
53+
result.push(dict.indexOf(lastWordAndCompression) + 1);
54+
return result;
55+
}

0 commit comments

Comments
 (0)