[Programmers] 코딩테스트 입문 120869. 외계어 사전

이지현·2023년 3월 9일
0

Algorithm

목록 보기
65/81
post-thumbnail

✔️ Problem URL

외계어 사전


✔️ Problem

PROGRAMMERS-962 행성에 불시착한 우주비행사 머쓱이는 외계행성의 언어를 공부하려고 합니다. 알파벳이 담긴 배열 spell과 외계어 사전 dic이 매개변수로 주어집니다. spell에 담긴 알파벳을 한번씩만 모두 사용한 단어가 dic에 존재한다면 1, 존재하지 않는다면 2를 return하도록 solution 함수를 완성해주세요.


✔️ Code

class Solution {
    public int solution(String[] spell, String[] dic) {
        int answer = 2;
        Boolean isExist = false;
        
        for(int i = 0; i < dic.length; i++) {            
            for(int j = 0; j < spell.length; j++) {
                if (dic[i].contains(spell[j]) == false) {
                    break;
                }
                if (j == spell.length-1) {
                    isExist = true;
                }
            }
            if(isExist) {
                answer = 1;
                break;
            }
        }
        return answer;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글