ALGORITHM/PROGRAMMERS

[프로그래머스] 영어 끝말잇기 (Summer/Winter Coding(~2018))

0298 2021. 7. 22. 17:05

https://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

2021-07-22


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
import java.util.HashSet;
 
public class Solution12981 {
    public static int[] solution(int n, String[] words) {
        HashSet<String> set = new HashSet<>();
        for(int i = 0; i < words.length; i++) {
            if(set.contains(words[i])) {
                return new int[]{i%n+1, i/n+1};
            } else {
                if(i > 0 && words[i-1].charAt(words[i-1].length()-1!= words[i].charAt(0)) return new int[]{i%n+1, i/n+1};
                set.add(words[i]);
            }
        }
        return new int[]{0,0};
    }
 
    public static void main(String[] args) {
        int n = 2;
        String[] w = {"hello""one""even""never""now""world""draw"};
        System.out.println(Arrays.toString(solution(n, w)));
    }
}
cs

 

#문제풀이

1. HashSet을 이용해서 중복 체크를 해준다

 

2. 중복이 아닌 경우, 끝말잇기가 제대로 되는지 체크해준다.