Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ca328a

Browse files
committed
이예진: [PG] 17686 파일명 정렬_241129
1 parent f0d09a3 commit 8ca328a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

‎Programmers/Level2/YJ_17686.java‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /