반응형
문제
풀이
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
반응형
'IT > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 삼각형의 완성조건 (2) - Java (0) | 2023.08.25 |
---|---|
[프로그래머스] 안전지대 - Java (1) | 2023.08.21 |
[프로그래머스] 다항식 더하기 - Java (0) | 2023.08.09 |
[프로그래머스] 최댓값 만들기 (2) - Java (0) | 2023.08.09 |
[프로그래머스] 캐릭터의 좌표 - Java (0) | 2023.07.17 |