[알고리즘문제풀기] 외계어 사전

silver's avatar
Jan 25, 2025
[알고리즘문제풀기] 외계어 사전

문제

내가 작성한 정답

import java.util.*; class Solution { public int solution(String[] spell, String[] dic) { String sortedSpell = String.join("", Arrays.stream(spell).sorted().toArray(String[]::new)); for (String s : dic) { char[] chars = s.toCharArray(); Arrays.sort(chars); String sortedDicWord = new String(chars); if (sortedSpell.equals(sortedDicWord)) { 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; } }
notion image
Share article

silver