Skip to content

Commit 3dbc384

Browse files
committed
solve : programmers 영어 끝말잇기
1 parent 3cabe48 commit 3dbc384

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/do02reen24/Review/week15.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
- 조합의 개념을 사용하여 풀 수 있었다.
66
- 해당 옷을 아예 고르지 않는 경우도 있으므로 각 아이템 길이에 + 1 을 해주었다.
77
- 모든 옷을 안고르는 경우는 제외해야하므로 정답에서 - 1 을 해주었다.
8+
9+
## :ballot_box_with_check: 프로그래머스 영어 끝말잇기(12981)
10+
11+
- 각 경우를 고려하여 처리해주었다.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const failUser = (n, index) => {
2+
let person = index % n;
3+
let order = Math.floor(index / n) + 1;
4+
if (person === 0) {
5+
person = n;
6+
order -= 1;
7+
}
8+
return [person, order];
9+
};
10+
11+
const getLastChar = (word) => word.charAt(word.length - 1);
12+
13+
const solution = (n, words) => {
14+
const dictionary = {};
15+
let lastChar = words[0][0];
16+
for (let index = 0; index < words.length; index += 1) {
17+
const word = words[index];
18+
if (lastChar === word[0] && dictionary[word] === undefined) {
19+
dictionary[word] = true;
20+
lastChar = getLastChar(word);
21+
continue;
22+
}
23+
return failUser(n, index + 1);
24+
}
25+
26+
return [0, 0];
27+
};
28+
29+
console.log(
30+
solution(2, ['hello', 'one', 'even', 'never', 'now', 'world', 'draw'])
31+
);

0 commit comments

Comments
 (0)