Algorithm/programmers

[programmers][Level1][Java] ์‹ ๊ทœ์•„์ด๋”” ์ถ”์ฒœ

youn12 2021. 3. 25. 17:43
โœ๏ธ Algorithm.

https://programmers.co.kr/learn/courses/30/lessons/72410
์ œํ•œ์‚ฌํ•ญ
์ž…์ถœ๋ ฅ ์˜ˆ


๐Ÿ“‹ Solved.

1. ๊ฐ๊ฐ์˜ ๋‹จ๊ณ„์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๊ตฌ์„ฑํ•˜์—ฌ ์ฒ˜๋ฆฌ 
2. ์ •๊ทœ์‹์„ ํ™œ์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํžˆ ๋  ๊ฒƒ๊ฐ™๋‹ค.


โœ”๏ธ Code.

public String solution(String new_id) {

        String answer = "";

        /*
         * 1. ๋Œ€๋ฌธ์ž -> ์†Œ๋ฌธ์ž ์น˜ํ™˜
         * */
        answer = new_id.toLowerCase();

        /*
         * 2. ์†Œ๋ฌธ์ž, ์ˆซ์ž, -, _, . ๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž ์ œ๊ฑฐ
         * */
        answer = answer.replaceAll("[^0-9a-z.\\-_]", "");

        /*
         * 3. .๋ฌธ์ž 2๋ฒˆ ์—ฐ์† -> . ํ•˜๋‚˜๋กœ ์น˜ํ™˜
         * */
        while(answer.indexOf("..") > -1){
            answer = answer.replace("..", ".");
        }

        /*
         * 4. .๋ฌธ์ž ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•  ๊ฒฝ์šฐ ์ œ๊ฑฐ
         * */
        if(answer.charAt(0) == '.') {
            answer = answer.substring(1);
        }
        if(answer.length() > 0 && answer.charAt(answer.length()-1) == '.') {
            answer = answer.substring(0, answer.length()-1);
        }

        /*
         * 5. ๋นˆ ๋ฌธ์ž์—ด -> a ๋Œ€์ž…
         * */
        if("".equals(answer)) answer = "a";

        /*
         * 6. ๋ฌธ์ž์—ด์ด 16์ž ์ด์ƒ์ด๋ฉด 15๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž ์ œ๊ฑฐ, ์ œ๊ฑฐ ํ›„ . ๋ฌธ์ž๊ฐ€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฉด .๋ฌธ์ž ์ œ๊ฑฐ
         * */
        if(answer.length() >= 16){
            answer = answer.substring(0, 15);
        }
        if(answer.length() > 0 && answer.charAt(answer.length()-1) == '.') {
            answer = answer.substring(0, answer.length()-1);
        }

        /*
         * 7. ๋ฌธ์ž์—ด์ด 2์ž ์ดํ•˜๋ผ๋ฉด, ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๊ธธ์ด๊ฐ€ 3์ž ๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
         * */
        while(answer.length() <= 2){
            char c = answer.charAt(answer.length()-1);
            answer += c;
        }

        return answer;
    }

 

'Algorithm > programmers' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[programmers][Level2][Java] ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ  (0) 2021.03.17