본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 영어 끝말잇기

by 현장 2025. 2. 28.

-Code

import java.util.*;

class Solution {
    public int[] solution(int n, String[] words) {
        int wrong = 0;
        int num = 0;
        int turn = 0;
        List<String> wordsList = new ArrayList<>();

        for (int i = 0; i < words.length; i++) {
            if (!wordsList.isEmpty()) {
                if (wordsList.contains(words[i])) {
                    wrong = i;
                    break;
                }
                String prev = wordsList.get(wordsList.size() - 1);
                if (words[i].charAt(0) != prev.charAt(prev.length() - 1)) {
                    wrong = i;
                    break;
                }
            }
            wordsList.add(words[i]);
        }
        if (wrong != 0) {
            num = wrong % n + 1;
            turn = (int) Math.ceil((wrong + 0.5) / n);
        }
        return new int[] {num, turn};
    }
}