프로그래머스

프로그래머스(자바, Java) - Level2. 영어 끝말잇기

seok02 2022. 12. 8. 10:26

https://school.programmers.co.kr/learn/courses/30/lessons/12981

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


주요 포인트

  • 탈락자가 나오지 않을 수도 있다.
  • 사람은 숫자를 1부터 세지만, 컴퓨터는 0부터 숫자를 센다.
  • 탈락자가 나오는 조건은 2가지다. 중복된 단어를 말하거나, 앞 단어의 마지막 문자로 시작하는 단어를 말하지 않았을 경우

 

소스코드

public int[] solution(int n, String[] words) {
        int[] answer = {};
        answer = new int[2];

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(words[0]);
        boolean fail = false;
        for (int j = 1; j < words.length; j++) {
            if (!arrayList.contains(words[j]) &&
                    arrayList.get(j - 1).substring(arrayList.get(j - 1).length() - 1).equals(words[j].substring(0, 1))) {
                arrayList.add(words[j]);
            } else {
                fail = true;
                answer[0] = (j % n) + 1;
                answer[1] = (j / n) + 1;
                break;
            }
        }

        if (fail == false) {
            answer[0] = 0;
            answer[1] = 0;
        }
        return answer;
    }

 

 

채점 결과

 

다른 물제 풀이 모음 : https://github.com/DOSEOKYEONG/programmers/tree/master/src/main/java/org/example (추가 중)