Programmers/Lv.1

프로그래머스 Lv.1 - 신규 아이디 추천

junnrecorder 2023. 10. 29. 15:32

https://school.programmers.co.kr/learn/courses/30/lessons/72410

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. 문제에 주어진 조건들에 맞춰 하나씩 작성을 하고, 정규식을 이용하여 문제를 해결한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public String solution(String newId) {
        newId = newId.toLowerCase();
        newId = newId.replaceAll("[^a-z0-9\\-_.]""");
        newId = newId.replaceAll("\\.+"".");
        newId = newId.replaceAll("^\\.+|\\.+$""");
        if(newId.isEmpty()) newId = "a";
        if(newId.length() >= 16) {
            newId = newId.substring(015);
            newId = newId.replaceAll("\\.+$""");
        }
        
        while(newId.length() < 3) {
            newId += newId.charAt(newId.length() - 1);
        }
        
        return newId;
    }
}
cs