본문 바로가기
코딩테스트/프로그래머스

[2021 KAKAO BLIND RECRUITMENT] 신규 아이디 추천 - JAVA

by 의정부핵꿀밤 2022. 9. 10.
728x90

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

 

프로그래머스

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

programmers.co.kr


이 문제는 크게 어렵진 않았다

아니 쉬웠다ㅋㅎㅋㅎㅋㅎ

그냥 정규식만 잘 사용하면 큰 어려움 없이 바로 해결할 수 있었다

사실 난 정규식을 이번에 제대로 써봤다

써보니까 별거 없드라~

코드 보면 바로 이해될 것이다!

 

 

 

자바 코드)

import java.util.*;

class Solution {
    public String solution(String new_id) {
        new_id = new_id.toLowerCase();
        
        new_id = new_id.replaceAll("[^a-z0-9-_.]","");
        
        while(new_id.contains("..")) {
            new_id = new_id.replace("..", ".");    
        }
        
        if (new_id.length() > 0) {
            if (new_id.charAt(0) == '.') {
                new_id = new_id.substring(1, new_id.length());
            }
        }
        if (new_id.length() > 0) {
            if (new_id.charAt(new_id.length() - 1) == '.') {
                new_id = new_id.substring(0, new_id.length() - 1);
            }
        }
        
        if(new_id.length()==0) {
            new_id = "a";
        }
        
        if(new_id.length()>=16) {
            new_id = new_id.substring(0, 15);
            if (new_id.charAt(new_id.length() - 1) == '.') {
                new_id = new_id.substring(0, new_id.length() - 1);
            }
        }
        
        if(new_id.length()<=2) {
            char last = new_id.charAt(new_id.length() - 1);
            while (new_id.length() < 3) {
                new_id += last;
            }
        }
        return new_id;
    }
}
728x90

댓글