영어끝말잇기 [JAVA]

LeHoODU·2024년 1월 30일
0
post-thumbnail

public int[] solution(int n, String[] words) {
        boolean t = true;
        int[] answer = {0,0};
        char st;
        char ed;
        int cnt = 0;
        Map<Integer, Integer> tf = new HashMap<>();
        List<String> word = new ArrayList<>();
        st = words[1].toCharArray()[0];
        ed = words[0].toCharArray()[words[0].toCharArray().length-1];
        
        while(t){
        	//
            if (cnt != words.length-1){
                if (word.contains(words[cnt])) {
                    tf.put(2, cnt);
                    tf.put(1, 0);
                    break;
                }
                if (st == ed){
                    st = words[cnt+1].toCharArray()[0];
                    ed = words[cnt].toCharArray()[words[cnt].toCharArray().length-1];
                    word.add(words[cnt]);
                    cnt +=1;
                    tf.put(1, cnt);
                    tf.put(2, 0);
                }
                else{
                    t = false;
                    tf.put(2, cnt);
                    tf.put(1, 0);
                }
            }else{
                if (word.contains(words[cnt])) {
                    tf.put(1, 0);
                    tf.put(2, cnt);
                    break;
                }
                else {
                    cnt+=1;
                    tf.put(2, 0);
                    tf.put(1, cnt);
                    t = false;
                }
            }

        }

        if (tf.get(1).equals(0)){
            if (cnt%n == 0 && words.length == cnt) {
                answer[0] =0;
                answer[1] =0;
            }
            else if (cnt < n){
                answer[0] = cnt +1;
                answer[1] = 1;
            }
            else if (cnt%n == 0 && words.length != cnt){
                answer[0] =1;
                answer[1] =cnt/n+1;
            }else{
                int a= cnt/n +1;
                int b =cnt%n+1;
                answer[0] = b;
                answer[1] = a;
            }
        }
        else {
            answer[0] =0;
            answer[1] =0;
        }
        
        if (cnt == 0){
            answer[0] =2;
            answer[1] =1;
        }

        return answer;
    }
profile
Back-End Developer

0개의 댓글