[프로그래머스] 외계어 사전 - Java

Yunki Kim·2023년 1월 9일
0

프로그래머스

목록 보기
84/101
post-thumbnail

문제


링크


코드

class Solution {
    public int solution(String[] spell, String[] dic) {
        boolean wordCheck = false;
        for (String word : dic) {
            int count = 0;
            for (String s : spell) {
                if (word.contains(s)) count++;
            }
            if (count == spell.length) {
                wordCheck = true;
                break;
            }
        }
        return wordCheck ? 1 : 2;
    }
}

리뷰

두 배열을 입력받아 spell 배열의 단어가 모두 포함된 dic속 단어가 있는지 찾는 문제이다.

단어를 하나씩 가져와서 문자열안에 해당 문자가 있는지 확인하고 spell 배열의 길이만큼
즉 배열의 모든 단어가 있으면 boolean 값을 가지는 wordCheck를 true값으로 바꿔준다.

0개의 댓글