프로그래머스 코딩테스트 입문 외계어 사전 [JAVA] - 22년 10월 7일

Denia·2022년 10월 7일
0

코딩테스트 준비

목록 보기
90/201

제한사항에서 제공되는 제한이 너무 널널해서 완전 탐색으로 풀이를 진행하는게 100배 빠르다.

스트림 사용

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

class Solution {
    public int solution(String[] spell, String[] dic) {
        List<Character> words = Arrays.stream(spell).map(s -> s.charAt(0)).collect(Collectors.toList());

        for (String str : dic) {
            if(str.length() < spell.length) continue;

            Set<Character> spellSet = str.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());

            if (spellSet.containsAll(words))
                return 1;
        }

        return 2;
    }
}

완전탐색

class Solution {
    public int solution(String[] spell, String[] dic) {
        int answer = 2;


        for(String dicS : dic) {

            boolean isRight = true;
            for(String spellS : spell) {
                if(dicS.indexOf(spellS) == -1) {
                    isRight = false;
                    break;
                }
            }

            if(isRight) {
                answer = 1;
                break;
            }
        }

        return answer;
    }
}

profile
HW -> FW -> Web

0개의 댓글