[JAVA/프로그래머스] 외계어 사전

윤소영·2024년 3월 13일
0

JAVA

목록 보기
31/41

🌟 문제



🍀 문제 답안

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



🍡 답안 풀이

  • contains() : boolean contains(CharSequence s)
    대상 문자열에 특정 문자열이 포함되어 있는지 확인하는 함수

string 배열을 사용해서 spell의 문자열이 dic에 포함되어있으면 count를 해서 spell의 length와 같으면 즉, 알파벳이 한단어에 다 있으면 answer = 1로 반환, 아니면 2로 반환.

1분만에 푼 문젠데 전엔 왤케 오래 걸린건지 참...!

0개의 댓글