[프로그래머스 / Java] Lv2 - 영어 끝말잇기

은상현(COBI)·2022년 12월 26일
0

Programmers

목록 보기
7/7
post-thumbnail

🔒 영어 끝말잇기

✔ 문제 설명

🚩 요구사항 분석

  • substring을 이용해 앞사람이 말한 마지막 문자 체크
  • 임의의 count 변수를 활용해 인원수와 같으면 차례를 추가해 탈락자 확인

🔑문제풀이

class Solution {
    public int[] solution(int n, String[] words) {
        int[] answer = new int[2];
        List<String> gameList = new ArrayList<>();
        int count = 0;
        int care = 1;
        for(int i = 0; i<words.length; i++){
            count++;
            if(!gameList.contains(words[i])){
                gameList.add(words[i]);
            } else{
                break;
            }
            
            if(i==0){
                continue;
            }
            String s= words[i-1].substring(words[i-1].length()-1,words[i-1].length());
            String p= words[i].substring(0,1);
            
            if(!s.equals(p)){
                break;
            }
           
            if(count == n){
                count = 0;
                care++;
            }
        }
        if(count != 0 ){       
            answer[0] = count;
            answer[1] = care;       
        }
        return answer;
    }
}

💡 추가한 테스트 케이스

ParametersReturn

추가한 테스트 케이스는 없다.

profile
백엔드 떠오르는 상현달

0개의 댓글