Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags more
Archives
Today
Total
관리 메뉴

공부블로그

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

IT/알고리즘

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

So1_b 2022. 7. 15. 01:06

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.*;

class Solution {
    public String solution(String new_id) {
       String[] arr = new_id.toLowerCase().split("");
		
		String alphabet = "abcdefghijklmnopqrstuvwxyz";
		String numbers	= "0123456789";
		String sChar	= "-_.";
		
		StringBuilder sb= new StringBuilder(new_id.length());
		//step 2
		for(String s: arr) {
			if(alphabet.contains(s))	sb.append(s);
			else if(numbers.contains(s))	sb.append(s);
			else if(sChar.contains(s))	sb.append(s);
			else continue;
		}
		System.out.println(sb);
		
		//step3
		while(true) {
			int i = sb.indexOf("..");
			if(i>-1) sb.replace(i, (i+2), ".");
			else break; //sb에 .. 포함x
		}System.out.println("step3: "+sb);
		
		//step4
		int length = sb.length();
		if(length>0 && sb.charAt(0)=='.') {
			sb.deleteCharAt(0);
			length = sb.length();
		}
		if(length>0 && sb.charAt(length-1)=='.') {
			sb.deleteCharAt(length-1);
			length = sb.length();
		}
		System.out.println("step4: "+sb);
		
		//step 5 ~ 6
		if(length == 0) sb.append("a");
		else if(length > 15) {
			sb.delete(15,length);
            if(sb.charAt(14)=='.') sb.deleteCharAt(14);
		}length = sb.length();
		System.out.println("step5~6: "+sb);
		
		//step 7
		while(length<3) { //1 ~ 2
			sb.append(sb.charAt(length-1));
			length = sb.length();
		}System.out.println("step7: "+sb);
		
		
        return sb.toString();
    }
}

'IT > 알고리즘' 카테고리의 다른 글

N Queen문제  (0) 2022.07.25
[프로그래머스] 예산  (0) 2022.07.18
[프로그래머스] 숫자 문자열과 영단어  (0) 2022.07.08
[프로그래머스] 1차 비밀지도  (0) 2022.07.07
[프로그래머스] 신고결과 받기  (0) 2022.06.18
Comments