[Programmers] 신규 아이디 추천

JongSeong Yang·2021년 4월 26일
0

programmers

목록 보기
1/16

문제 풀이 : 2021.04.26

비효율적인 방법

class Solution {
    public String solution(String new_id) {
        String answer = "";
        String id = new_id;
        // step1 
        id = id.toLowerCase();

        // step2
        byte[] bt = id.getBytes();
        for(int i = 0; i<id.length();i++){
            String tmp = id.substring(i,i+1);
            if(tmp.equals("-") || tmp.equals("_") || tmp.equals(".") || (bt[i]>=97 && bt[i]<=122) || (bt[i] >=48 && bt[i]<=57))
                continue;
            id = id.replace(tmp," ");
        }

        answer = removeSpace(id);
        answer = removeHeadTail(answer);
        answer = removeDuplicate(answer);
        if(answer.length()>=16 || answer.length() <=2){
            answer = makeLength(answer);
        }
        return answer;
    }
    // step3
    static String removeDuplicate(String id){
        String result = id;
        while(result.contains("..")){
            result = result.replace("..",".");
        }
        return result;
    }

    // step6,7
    static String makeLength(String id){
        String result = "";
        if(id.length()>=16)
            result = id.substring(0,15);
        if(id.length()<=2){
            result = id;
            for(int i = 0;i<3-id.length();i++)
                result+=id.substring(id.length()-1, id.length());
        }
        result = removeHeadTail(result);
        return result;
    }

    static String removeSpace(String id){
        String result="";
        String[] a = id.split(" ");
        for(int i = 0;i<a.length;i++){
            result+=a[i].trim();
        }
        return result;
    }

    // step 4,5
    static String removeHeadTail(String id){
        char[] a = id.toCharArray();
        String result = id;
        while(true){
            if(!result.startsWith(".") && !result.endsWith(".")) break;
            if(result.startsWith(".")) a[0] = ' ';
            if(result.endsWith(".")) a[a.length-1] = ' ';

            result = String.valueOf(a);
            result = result.trim();
            a = result.toCharArray();
        }
        if(result.length()==0) return "a";
        return result = String.valueOf(a);
    }
}

효율적인 방법

class Solution {
    public String solution(String new_id) {
        String answer = new_id.toLowerCase(); // step1

        answer = answer.replaceAll("[^-_.a-z0-9]", ""); // step2
        answer = answer.replaceAll("[.]{2,}", "."); // step3
        answer = answer.replaceAll("^[.]|[.]$", "");    // step4
        
        if (answer.equals("")) {    // step5
            answer += "a";
        }

        if (answer.length() >= 16) {     // step6
            answer = answer.substring(0, 15);
            answer = answer.replaceAll("[.]$","");
        }

        if (answer.length() <= 2) {  // step7
            while (answer.length() < 3) {
                answer += answer.charAt(answer.length()-1);
            }
        }
        return answer;
    }
}

문제 출처 링크

profile
꿈꾸는 개발자

0개의 댓글