[2021 카카오 블라인드] 신규 아이디 추천 (JAVA)

Jiwoo Kim·2021년 5월 7일
0
post-thumbnail

문제


풀이

항상 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;
    }
}

0개의 댓글