항상 String 처리를 묻는 카카오 문제... 시험 전날이니까 연습하기 위해 이 문제를 풀어보았다. 정규식을 배우긴 했지만, 원하는 조건을 즉시 정규식으로 표현하지는 못해서 아쉬웠다. 이번을 기회로 삼아서 확실히 알아 놓아야겠다.
class Solution {
private static final char DOT = '.';
private static final String BLANK = "";
private static final String NEW_ID_FOR_EMPTY_STRING = "a";
private static final int MAX_LENGTH = 15;
private static final int MIN_LENGTH = 3;
public String solution(String new_id) {
// 1단계
String result = new_id.toLowerCase();
// 2단계
result = result.replaceAll("[^-_.a-z0-9]", BLANK);
// 3단계
result = result.replaceAll("[.]{2,}", DOT + BLANK);
// 4단계
result = result.replaceAll("^[.]|[.]$", BLANK);
// 5단계
result = (result.isEmpty() ? NEW_ID_FOR_EMPTY_STRING : result);
// 6단계
result = (result.length() > MAX_LENGTH ? result.substring(0, MAX_LENGTH).replaceAll("^[.]|[.]$", BLANK) : result);
// 7단계
result = (result.length() < MIN_LENGTH ? extendToMinLength(result) : result);
return result;
}
private String extendToMinLength(String result) {
char ch = result.charAt(result.length() - 1);
while (result.length() < MIN_LENGTH) result += ch;
return result;
}
}