forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYJ_17686.java
More file actions
56 lines (51 loc) · 2.09 KB
/
YJ_17686.java
File metadata and controls
56 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//정규식 문자열
public class YJ_17686 {
static class File implements Comparable<File>{
String head;
String num;
String tail;
public File(String head, String num, String tail) {
this.head = head;
this.num = num;
this.tail = tail;
}
@Override
public int compareTo(File o) {
//1. HEAD 사전순으로 정렬, 대소문자 구분X 2. NUMBER의 숫자 순으로 정렬
int order = this.head.toLowerCase().compareTo(o.head.toLowerCase());
return order == 0 ? Integer.parseInt(this.num) - Integer.parseInt(o.num) : order;
}
}
public String[] solution(String[] files) {
List<File> fileList = new ArrayList<>();
//HEAD는 숫자가 아닌 문자로 이루어져 있으며, 최소한 한 글자 이상이다.
//NUMBER는 한 글자에서 최대 다섯 글자 사이의 연속된 숫자 (0~9)
final String regex = "([a-zA-Z\\s\\-]+)([0-9]{1,5})(.*)";
for(String file : files){
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(file);
while(matcher.find()){
//TAIL은 자가 다시 나타날 수도 있으며, 아무 글자도 없을 수 있다.
if(matcher.group(3).isEmpty()){
fileList.add(new File(matcher.group(1),matcher.group(2),""));
}else{
fileList.add(new File(matcher.group(1),matcher.group(2),matcher.group(3)));
}
}
}
Collections.sort(fileList);
//파일명 조합
String[] answer = new String[fileList.size()];
StringBuilder sb = new StringBuilder();
for(int i = 0; i < fileList.size(); i++){
answer[i] = sb.append(fileList.get(i).head)
.append(fileList.get(i).num)
.append(fileList.get(i).tail).toString();
sb.setLength(0);
}
return answer;
}
}