문제
내가 작성한 정답
class Solution {
public int solution(String[] spell, String[] dic) {
for(String s:dic){
for(int i=0;i<spell.length;i++){
if(!s.contains(spell[i])) break;
else {
if (i==spell.length-1) return 1;
}
}
}
return 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;
}
}
import java.util.Arrays;
import java.util.stream.Collectors;
class Solution {
public int solution(String[] spell, String[] dic) {
return Arrays.stream(dic)
.map(s -> s.chars()
.sorted()
.mapToObj(i -> String.valueOf((char) i))
.collect(Collectors.joining())).
collect(Collectors.toList())
.contains(Arrays.stream(spell)
.sorted()
.collect(Collectors.joining())) ? 1 : 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;
}
}Share article