끄적끄적 코딩일지

[Programmers]신규 아이디 추천(난이도:★★★★) 본문

알고리즘

[Programmers]신규 아이디 추천(난이도:★★★★)

BaekGyuHyeon 2022. 5. 16. 20:10

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

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr

class Solution {
    public String solution(String new_id) {
      	String t =new_id;
		t = t.toLowerCase() //step 1
            .replaceAll("[^-_.a-z0-9]", "") // step 2
            .replaceAll("[.]{2,}", ".") // step 3
            .replaceAll("^[.]", "").replaceAll("[.]$", ""); // step 4
		if (t.equals(""))
			t = "a"; // step 5
		if (t.length() > 15)
			t = t.substring(0, 15).replaceAll("^[.]", "").replaceAll("[.]$", ""); // step 6
		if (t.length() < 3) {
            // step 7
			int repeatCount = 3 - t.length();
			String l = new String(new char[] { t.charAt(t.length() - 1) }).repeat(repeatCount);
			t = t + l;
		}
        return t;
    }
}