From 1535108a1f7fbff68ee3adb51b1a04da48a47aef Mon Sep 17 00:00:00 2001 From: eve712 <62237639+eve712@users.noreply.github.com> Date: Tue, 22 Dec 2020 16:38:59 +0900 Subject: [PATCH] =?UTF-8?q?=EC=83=9D=EC=84=B1:=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EB=9E=98=EB=A8=B8=EC=8A=A4=20=EC=98=81=EC=96=B4=20?= =?UTF-8?q?=EB=81=9D=EB=A7=90=EC=9E=87=EA=B8=B0=20=ED=92=80=EC=9D=B4=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithm/pr12981(word_chain_game).js | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 algorithm/pr12981(word_chain_game).js diff --git a/algorithm/pr12981(word_chain_game).js b/algorithm/pr12981(word_chain_game).js new file mode 100644 index 0000000..70b6f90 --- /dev/null +++ b/algorithm/pr12981(word_chain_game).js @@ -0,0 +1,43 @@ +function solution(n, words) { + let answer = []; + let data = []; + for(let i = 0; i < n; i++) { data.push(new Array()); } + + const times = Math.ceil(words.length / n); + outer: for(let i = 0; i < times; i++) { + for(let j = 0; j < n; j++) { + if(words[j]) { + data[j].push(words[j]); + const condition = isRepeated(j, data, words[j]) || !isCorrectChar(j, n, data, words[j]) + if(condition) { + answer.push(j+1, data[j].length); + break outer; + } + } + } + words.splice(0, n); + } + if(answer.length === 0) answer = [0, 0]; + console.log(answer); + return answer; +} +function isCorrectChar(j, n, data, word) { + if(j === 0) j = n; + const idx = data[j-1].length - 1; + if(idx === -1) return true; + const previousChar = data[j-1][idx].substr(-1, 1); + const nowChar = word.substr(0, 1); + return previousChar === nowChar; +} +function isRepeated(j, data, word) { + const copied = data.map(e => [...e]); + copied[j].splice(-1, 1); + const spreadData = copied.flat(1); + return spreadData.indexOf(word) !== -1 +} + +// let n = 3; +// let words = ['tank', 'kick', 'know', 'wheel', 'land', 'dream', 'mother', 'robot', 'tank']; +let n = 2; +let words = ["hello", "one", "even", "never", "now", "world", "draw"]; +solution(n, words);