알고리즘

프로그래머스 영어 끝말잇기

오래먹는오레오 2021. 1. 11. 14:00

영어 끝말잇기 : programmers.co.kr/learn/courses/30/lessons/12981

 

코딩테스트 연습 - 영어 끝말잇기

3 [tank, kick, know, wheel, land, dream, mother, robot, tank] [3,3] 5 [hello, observe, effect, take, either, recognize, encourage, ensure, establish, hang, gather, refer, reference, estimate, executive] [0,0]

programmers.co.kr

N명이서 끝말잇기를 하는데 영어로 하는 게임이다.

룰은 기본 국제 룰과 같다.

 

import java.util.*;
class Solution {
    public Set<String> set = new HashSet<String>();

	public int[] solution(int n, String[] words) {
		int[] answer = new int[] { 0, 0 };
		int count = 1;
		int turn = 1;
		char lastChar = words[0].charAt(words[0].length() - 1);
		set.add(words[0]);

		for (int i = 1; i < words.length; i++) {
			if (set.contains(words[i]) || lastChar != words[i].charAt(0)) {
				answer = new int[] { turn + 1, count };
				break;
			}

			set.add(words[i]);
			lastChar = words[i].charAt(words[i].length() - 1);
			++turn;
			turn %= n;
			if (turn == 0) {
				count++;
			}
		}
		return answer;
	}
}