# BOJ01181 단어 정렬
https://www.acmicpc.net/problem/1181 (opens new window)
알파벳 소문자로 이루어진 N개의 단어를 아래 조건에 충족시키도록 정렬해야 한다.
- 길이가 짧은 것부터
- 길이가 같으면 사전 순으로
Arrays.sort(words, (x, y) -> {
if (x.length() < y.length()) { // 길이 오름차순 정렬
return -1;
} else if (x.length() == y.length()) { // 길이가 같다면 사전순 정렬
return x.compareTo(y);
}
return 1;
});
WARNING
단, 같은 단어가 여러 번 입력된 경우에는 한 번씩만 출력한다.
String prevWord = "";
for (String word : words) {
if (!prevWord.equals(word)) {
System.out.println(word);
}
prevWord = word;
}
연속되지 않는 단어만 골라 출력한다.
import java.util.Arrays;
import java.util.Scanner;
public class BOJ1181 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] words = new String[n];
for (int i = 0; i < n; i++) {
words[i] = scanner.next();
}
Arrays.sort(words, (x, y) -> {
if (x.length() < y.length()) {
return -1;
} else if (x.length() == y.length()) {
return x.compareTo(y);
}
return 1;
});
String prevWord = "";
for (String word : words) {
if (!prevWord.equals(word)) {
System.out.println(word);
}
prevWord = word;
}
}
}