IT/프로그래머스

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

짐99 2023. 8. 25. 15:22
반응형

 

 

 

문제

 

 

 

 

 

풀이

cnt 배열을 사용해서 dic의 단어마다 spell의 해당하는 알파벳이 있을 때, cnt 배열을 ++ 해준다.

spell의 갯수만큼 ++이 되면 spell의 알파벳이 모두 해당하는 것으로 인식하고 1을 리턴한다.

 

 

코드

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

 

 

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

반응형