Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 압축-카카오 문제 reduce 활용 풀이 추가 #102

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions level-2/[3차]-압축&17684&.js
Original file line number Diff line number Diff line change
@@ -31,3 +31,25 @@ function solution(msg) {
}
return outputs;
}

// 정답 2 - ssi02014
function solution(msg) {
const result = [];
const dict = Array.from({length: 26},(_, i) => String.fromCharCode(65 + i))

// 시간 복잡도 O(N^2)
const lastWordAndCompression = msg.split("").reduce((acc, cur) => {
const nextWord = acc + cur;
const nextWordIdx = dict.indexOf(nextWord);
const prevWordIdx = dict.indexOf(acc);

if (nextWordIdx !== -1) return acc + cur;
dict.push(nextWord);

if (prevWordIdx !== -1) result.push(prevWordIdx + 1);
return cur;
}, "");
Comment on lines +41 to +51
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와우 너무 멋진 풀이네요!!! reduce 메서드를 이번 풀이에 멋지게 사용하신 것 같습니다 배워갑니다!! 😄


result.push(dict.indexOf(lastWordAndCompression) + 1);
return result;
}