[프로그래머스][java] 신규 아이디 추천

김현진·2022년 1월 16일
0

코테준비

목록 보기
15/22
  • 문제 해결:
    문제에서 주어진 단계를 그대로 따라가면 된다.
    각 단계에서 어떻게 주어진 방식대로 바꿀수 있을까만 잘 생각하면 된다.
import java.lang.Character;

class Solution {
    public String solution(String aid) {
        
        aid = aid.toLowerCase(); //1단계
        
        char [] id = aid.toCharArray();
        
        
        for(int i=0;i<id.length;i++){ //2단계
            if(!(id[i]=='.' || id[i]=='-' || id[i]=='_' || (id[i]>=48&&id[i]<=57) || Character.isLowerCase(id[i]))){
                id[i]=' ';
            }
        }
        
        String step2 = String.valueOf(id);
        String step3 = step2.replace(" ","");
        
        StringBuilder sb = new StringBuilder();
        
        for(int i=0;i<step3.length();i++){ //3단계
            if(!(step3.length()-1==i)&&
            step3.charAt(i)=='.'&&step3.charAt(i+1)=='.'){ 
            //현재 문자가 .일때 이전 문자가 .인지 확인하고 맞다면 통과
                continue;
            }else{ //아니면 문자열에 추가
                sb.append(step3.charAt(i));
            }
        }
        
        String step4 = new String(sb.toString());
        
        
        StringBuilder sb2 = new StringBuilder();
        for(int i=0;i<step4.length();i++){ //4단계
            if(i==0&&step4.charAt(i)=='.') continue; //처음에 .이 있을 때
            else if(i==(step4.length()-1) && 
            step4.charAt(i)=='.') {continue;} //마지막에 .이 있을 때
            else sb2.append(step4.charAt(i)); //아닐때
            
        }
        
        String step5 = new String(sb2.toString());
        
        
        if(step5.length()==0){ //5단계
            step5 = "a";
        }
        
        
        if(step5.length()>=16){ //6단계
            step5 = step5.substring(0,15);
        }
        
        
        if(step5.charAt(step5.length()-1)=='.'){ //6단계
            step5 = step5.substring(0,step5.length()-1);
        }
        
        if(step5.length()==1){ //7단계
            step5+=step5.charAt(step5.length()-1);
            step5+=step5.charAt(step5.length()-1);
        } else if(step5.length()==2){
            step5+=step5.charAt(step5.length()-1);
        }
        
        
        return step5;
    }
}

0개의 댓글