230705 영어 끝말잇기

Jongleee·2023년 7월 5일
0

TIL

목록 보기
303/576
public int[] solution(int n, String[] words) {
	HashSet<String> set = new HashSet<>();
	set.add(words[0]);
	for (int i = 1; i < words.length; i++) {
		String previousWord = words[i - 1];
		String currentWord = words[i];

		char last = previousWord.charAt(previousWord.length() - 1);
		char first = currentWord.charAt(0);

		if (set.contains(currentWord) || last != first) {
			int setNum = set.size();
			return new int[] { setNum % n + 1, setNum / n + 1 };
		}
		set.add(currentWord);
	}
	return new int[] { 0, 0 };
}

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

0개의 댓글