문제
전화번호목록
문제풀이
import java.util.*;
class Solution {
public boolean solution(String[] phone_book) {
boolean answer = true;
Arrays.sort(phone_book, new Comparator<String>(){
public int compare(String s1, String s2) {
if (s1.length() == s2.length()) {
return s1.compareTo(s2);
}
else {
return s1.length() - s2.length();
}
}
});
Boolean flag = false;
int max_length = phone_book[0].length();
for (int i = 1; i < phone_book.length; i++) {
if (max_length < phone_book[i].length()) {
flag = true;
break;
}
}
if (flag) {
loop:
for (int i = 0; i < phone_book.length; i++) {
for (int j = i + 1; j < phone_book.length; j++) {
String keyword = phone_book[i] + ".*";
if (phone_book[j].matches(keyword)) {
answer = false;
break loop;
}
}
}
}
return answer;
}
}