From 0b6dc68bc5cfd9a62279df350d351f41e0bcf6f1 Mon Sep 17 00:00:00 2001 From: ssi02014 <ssi02014@naver.com> Date: Sun, 18 Sep 2022 15:18:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=95=95=EC=B6=95-=EC=B9=B4?= =?UTF-8?q?=EC=B9=B4=EC=98=A4=20=EB=AC=B8=EC=A0=9C=20reduce=20=ED=99=9C?= =?UTF-8?q?=EC=9A=A9=20=ED=92=80=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\250]-\354\225\225\354\266\225&17684&.js" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" "b/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" index d933e62..4b3ee2c 100644 --- "a/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" +++ "b/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" @@ -31,3 +31,52 @@ function solution(msg) { } return outputs; } + +// 정답 2 - ssi02014 +function solution(msg) { + const result = []; + const dict = [ + "A", + "B", + "C", + "D", + "E", + "F", + "G", + "H", + "I", + "J", + "K", + "L", + "M", + "N", + "O", + "P", + "Q", + "R", + "S", + "T", + "U", + "V", + "W", + "X", + "Y", + "Z", + ]; + + // 시간 복잡도 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; + }, ""); + + result.push(dict.indexOf(lastWordAndCompression) + 1); + return result; +} From 63d4698855d85df77a2deccd07082bd01132b604 Mon Sep 17 00:00:00 2001 From: ssi02014 <ssi02014@naver.com> Date: Wed, 21 Sep 2022 17:54:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refac:=203=EC=B0=A8=20=EC=95=95=EC=B6=95=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\250]-\354\225\225\354\266\225&17684&.js" | 29 +------------------ 1 file changed, 1 insertion(+), 28 deletions(-) diff --git "a/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" "b/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" index 4b3ee2c..75ad8eb 100644 --- "a/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" +++ "b/level-2/[3\354\260\250]-\354\225\225\354\266\225&17684&.js" @@ -35,34 +35,7 @@ function solution(msg) { // 정답 2 - ssi02014 function solution(msg) { const result = []; - const dict = [ - "A", - "B", - "C", - "D", - "E", - "F", - "G", - "H", - "I", - "J", - "K", - "L", - "M", - "N", - "O", - "P", - "Q", - "R", - "S", - "T", - "U", - "V", - "W", - "X", - "Y", - "Z", - ]; + const dict = Array.from({length: 26},(_, i) => String.fromCharCode(65 + i)) // 시간 복잡도 O(N^2) const lastWordAndCompression = msg.split("").reduce((acc, cur) => {