package programmers;
public class New_ID_recommendation {
public String solution(String new_id) {
String answer = step1(step1(new_id));
return answer;
}
public static String step1(String id) { // 대문자 -> 소문자
return step2(id.toLowerCase());
}
public static String step2(String id) {
char[] str = id.toCharArray();
for (int i = 0; i < str.length; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
continue;
if (str[i] >= '0' && str[i] <= '9')
continue;
if (str[i] == '-')
continue;
if (str[i] == '_')
continue;
if (str[i] == '.')
continue;
str[i] = ' ';
}
return step3(String.valueOf(str).replaceAll(" ", ""));
}
public static String step3(String id) {
char[] str = id.toCharArray();
for (int i = 0; i < str.length - 1; i++) {
if (str[i] == '.') {
int idx = i + 1;
while (true) {
if (idx == str.length)
break;
if (str[idx] == '.') {
str[idx] = ' ';
idx++;
} else
break;
}
}
}
return step4(String.valueOf(str).replaceAll(" ", ""));
}
public static String step4(String id) {
char[] str = id.toCharArray();
if (str[0] == '.')
str[0] = ' ';
if (str[str.length - 1] == '.')
str[str.length - 1] = ' ';
return step5(String.valueOf(str).trim());
}
public static String step5(String id) {
id = step6((id.equals("") ? "a" : id));
return id;
}
public static String step6(String id) {
id = step7((id.length() >= 16 ? id.substring(0, 15) : id));
return id;
}
public static String step7(String id) {
char c = id.charAt(id.length() - 1);
while (id.length() < 3) {
if (id.length() <= 2) {
id += c;
}
}
return id;
}
}
조건

하나하나 직접 구현 -> 성능 매우 안좋음
조건 6을 검사후 4단계를 다시 검사해봐야한다. (지워졌을때 마지막에 마침표가 남아있을수도 있기 때문)
조건 4를 다시 실행해야하지만 로직상 연속으로 호출하기때문에 step1함수를 그냥 두번 호출함 -> 성능 저하 발생
-> 해결방법 : 자바정규화사용
replaceAll("[^-_.a-z0-9]", ""); -> 조건2 대응
replaceAll("[.]{2,}", "."); -> 조건 3 대응
replaceAll("^[.]|[.]$",""); -> 조건 4대응
'휴지통 > 알고리즘 & 자료구조' 카테고리의 다른 글
| 백준 1157_1 (0) | 2022.03.11 |
|---|---|
| 백준 18108 (0) | 2022.03.10 |
| 신고 결과 받기 [Java] (0) | 2022.03.08 |
| 백준 2231 (0) | 2022.03.07 |
| 문제해결전략 : BOGGLE (Java) (0) | 2022.03.06 |