본문 바로가기

CS/Algorithm

신규 아이디 추천

반응형

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

 

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

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

programmers.co.kr

뭔 놈의 조건이 이리 많은지..

이 문제 하나 푸는데 40분정도 걸렸던것 같다..

package question;

public class RecomIdQuesition
{
    public static void main(String [] args)
    {
        System.out.println(solution("...!@BaT#*..y.abcdefghijklm"));
        System.out.println(solution("z-+.^."));
        System.out.println(solution("=.="));
        System.out.println(solution("123_.def"));
        System.out.println(solution("abcdefghijklmn.p"));
    }

    public static String solution(String new_id)
    {
        new_id = new_id.toLowerCase();
        new_id = new_id.replaceAll("[^a-z0-9-_.]", "");

        char [] array = new_id.toCharArray();
        int continous = 0;
        for (int i = 0; i<new_id.length(); i++)
        {
            if (array[i] == '.')
            {
                continous++;
                if (continous >= 2)
                {
                    array[i] = '&';
                }
            } else {
                continous = 0;
            }
        }

        new_id = new String(array);
        new_id = new_id.replaceAll("&", "");
        if (new_id.charAt(0) == '.')
        {
            new_id = new_id.substring(1);
        }
        if (!new_id.isEmpty() && 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 a = new_id.charAt(new_id.length()-1);
            for (int i = new_id.length(); i < 3; i++)
            {
                new_id+=a;
            }
        }
        return new_id;
    }
}
반응형

'CS > Algorithm' 카테고리의 다른 글

모의고사  (0) 2021.05.04
키패드 누르기  (0) 2021.05.04
k 번째 수  (0) 2021.05.03
완주하지 못한 선수  (0) 2021.04.30
뉴스 클러스터링  (0) 2021.04.29