본문 바로가기
  • Let's study
PS/Programmers

[프로그래머스 Lv.0] 외계어 사전(Java)

by 코딩고수이고파 2026. 8. 8.

문제

https://school.programmers.co.kr/learn/courses/30/lessons/120869

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

풀이

사전에 있는 단어를 하나씩 확인하면서 spell에 있는 모든 문자가 포함되어 있는지 검사하면 된다.

각 단어마다 check 변수를 0으로 초기화하고, spell의 문자들을 순서대로 확인한다. 단어에 해당 문자가 포함되어 있으면 check를 증가시킨다.

for (String str : dic) {
    int check = 0;

    for (int j = 0; j < spell.length; j++) {
        if (str.contains(spell[j])) {
            check++;
        }
    }
}

여기서 contains()를 이용해 특정 문자열이 포함되어 있는지 확인한다. spell의 모든 문자가 포함되어 있다면 check의 값은 spell.length와 같아진다.

if (check == spell.length) {
    answer = 1;
}

하나의 단어라도 조건을 만족하면 정답은 1이 되고, 끝까지 만족하는 단어가 없다면 초기값인 2를 반환한다.

코드

class Solution {
    public int solution(String[] spell, String[] dic) {
        int answer = 2;
        
        for(String str : dic){
            int check = 0;
            for(int j = 0;j<spell.length;j++){
                if(str.contains(spell[j])){
                    check++;
                }
                if(check == spell.length){
                    answer = 1;
                }
            }
        }
        
        return answer;
    }
}

댓글