Skip to content

Commit 31950c5

Browse files
committed
solve: programmers 영어끝말잇기
1 parent 4735848 commit 31950c5

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

  • src/kyu9341/programmers_영어끝말잇기
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const solution = (n, words) => {
2+
const set = new Set();
3+
let prevWrod = '';
4+
5+
for (const [idx, word] of words.entries()) {
6+
const beNotEqual =
7+
idx && prevWrod.charAt(prevWrod.length - 1) !== word.charAt(0);
8+
9+
if (set.has(word) || beNotEqual) {
10+
const num = (idx + 1) % n || n;
11+
const count = Math.floor((idx + 1) / n) + Number(num !== n);
12+
13+
return [num, count];
14+
}
15+
16+
set.add(word);
17+
prevWrod = word;
18+
}
19+
20+
return [0, 0];
21+
};
22+
23+
(() => {
24+
const inputs = [
25+
{
26+
n: 3,
27+
words: [
28+
'tank',
29+
'kick',
30+
'know',
31+
'wheel',
32+
'land',
33+
'dream',
34+
'mother',
35+
'robot',
36+
'tank',
37+
],
38+
},
39+
{
40+
n: 5,
41+
words: [
42+
'hello',
43+
'observe',
44+
'effect',
45+
'take',
46+
'either',
47+
'recognize',
48+
'encourage',
49+
'ensure',
50+
'establish',
51+
'hang',
52+
'gather',
53+
'refer',
54+
'reference',
55+
'estimate',
56+
'executive',
57+
],
58+
},
59+
{ n: 2, words: ['hello', 'one', 'even', 'never', 'now', 'world', 'draw'] },
60+
];
61+
62+
inputs.forEach(input => console.log(solution(input.n, input.words)));
63+
})();

0 commit comments

Comments
 (0)