
내가 작성한 코드
import java.util.*;
class Solution {
public int solution(String[] spell, String[] dic) {
for (String word : dic) {
Set<Character> set = new HashSet<>(); // 단어의 문자를 저장할 Set >> 중복 제거
for (char c : word.toCharArray()) {
set.add(c); // 단어의 문자들을 Set에 추가
}
boolean allContained = true;
for (String s : spell) {
if (!set.contains(s.charAt(0))) { // spell의 문자가 포함되지 않으면 false
allContained = false;
break;
}
}
if (allContained) return 1; // spell의 모든 문자가 포함된 단어를 찾으면 1 반환
}
return 2; // spell을 포함하는 단어가 없으면 2 반환
}
}
다른 사람의 코드
class Solution {
public int solution(String[] spell, String[] dic) {
for(int i=0;i<dic.length;i++){
int answer = 0;
for(int j=0;j<spell.length;j++){
if(dic[i].contains(spell[j])) answer ++;
}
if(answer==spell.length) return 1;
}
return 2;
}
}
Share article