: 같은 단어가 2번 나오면 탈락하게 되는 규칙이 있어 HashMap 을 사용하여 횟수를 체크했다.
구현
: 주어진 String[] words 를 for문을 통해 순회하며 단어가 2번 사용되는 경우, 단어가 지속되지 않는 경우를 체크한다.
체크된 경우에 i번 사람이 자신의 j번 차례인 것을 반환해야 한다.
n = 3이고 8번째에 탈락한다면 이는 3번 (8%3 + 1) 사람이 자신의 3번(8/3 + 1) 차례 라고 할 수 있다.
✅ 전체 코드
import java.util.*;
class Solution {
public int[] solution(int n, String[] words) {
// 카운트할 map
HashMap<String, Integer> count = new HashMap<>();
char last =words[0].charAt(0);
for(int i=0; i<words.length; i++) {
// 단어가 2번 사용되는 경우
if(count.containsKey(words[i])) {
// i번 사람이 자신의 j번 차례
return new int[] {i%n+1, i/n+1};
}
// 단어가 지속되지 않는 경우
if(last != words[i].charAt(0)) {
return new int[] {i%n+1, i/n+1};
}
count.put(words[i], 1);
last = words[i].charAt(words[i].length()-1);
}
return new int[] {0,0};
}
}