백준 1181 - 단어 정렬 (자바)

SlowAnd·2023년 12월 29일
0
public class boj_1181 {
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        int count = Integer.parseInt(r.readLine());

        Set<String> words = new HashSet<>();

        for(int i=0;i<count;i++){
            words.add(r.readLine());
        }
        List<String> collect = words.stream()
                .sorted(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()))
                .collect(Collectors.toList());

        for(String index : collect){
            w.write(index);
            w.newLine();
        }
        w.flush();
    }
}

0개의 댓글