๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

by ynzu๐Ÿค 2022. 7. 24.
๋ฐ˜์‘ํ˜•


๐Ÿ‘‡ ๋ฌธ์ œ URL ๐Ÿ‘‡

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


class Solution {
    public String solution(String new_id) {
        String answer = "";

        //1๋‹จ๊ณ„
        new_id = new_id.toLowerCase();
        System.out.println("1๋‹จ๊ณ„ :"+new_id);

        //2๋‹จ๊ณ„
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<new_id.length(); i++) {
            if (String.valueOf(new_id.charAt(i)).matches("[0-9a-z._-]")) {
                sb.append(new_id.charAt(i));
            }
        }
        new_id = sb.toString();
        System.out.println("2๋‹จ๊ณ„ :"+new_id);

        //3๋‹จ๊ณ„
        StringBuffer sb2 = new StringBuffer();
        int cnt = 0;
        for(int i=0; i<new_id.length(); i++) {

            if (new_id.charAt(i) == '.') {
                cnt++;

                if (cnt < 2) {
                    sb2.append(new_id.charAt(i));
                }
            } else {
                cnt = 0;
                sb2.append(new_id.charAt(i));
            }

        }

        new_id = sb2.toString();
        System.out.println("3๋‹จ๊ณ„ :"+new_id);

        if(new_id.charAt(0) == '.'){
            new_id = new_id.substring(1);
        }else if(new_id.charAt(new_id.length()-1) == '.'){
            new_id = new_id.substring(0, new_id.length()-1);
        }

        System.out.println("4๋‹จ๊ณ„ :"+new_id);

        if(new_id.length() == 0){
            new_id = "a";
        }
        System.out.println("5๋‹จ๊ณ„ :"+new_id);

        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);
            }
        }
        System.out.println("6๋‹จ๊ณ„ :"+new_id);

        if(new_id.length() ==2 ){
           new_id = new_id + new_id.charAt(1);
        }else if(new_id.length() ==1){
            new_id = new_id + new_id.charAt(0) + new_id.charAt(0);
        }
        System.out.println("7๋‹จ๊ณ„ :"+new_id);

        answer = new_id;
        return answer;
    }
}


๊ฒฐ๊ณผ..


2๋‹จ๊ณ„๋งŒ ์ •๊ทœ์‹์œผ๋กœ ํ’€์—ˆ๋Š”๋ฐ.. ์ฐพ์•„๋ณด๋‹ˆ 3,4,5 ๋‹จ๊ณ„๋„ ์ •๊ทœ์‹์œผ๋กœ ํ’€์—‡์–ด์•ผ ํ–ˆ๋‹ค..
2๋‹จ๊ณ„๋„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.

new_id = new_id.replaceAll("[^0-9a-z._-]", "");

[]์•ˆ์—์„œ ^๋Š” ๊ทธ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ๊ฒƒ๋“ค์„ ์–˜๊ธฐํ•œ๋‹ค.

3๋‹จ๊ณ„๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์˜€๋‹ค.

new_id = new_id.replaceAll("[.]{2,}", ".");

[a]{2}์ด๋ฉด aa์ด๊ณ  [a]{2,}์ด๋ฉด a๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๊ณ , [a]{2, 4}์ด๋ฉด aa, aaa, aaaa ์ด๋‹ค.

4๋‹จ๊ณ„๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์˜€๋‹ค.

new_id = new_id.replaceAll("^[.]|[.]$", "");

^ : ์‹œ์ž‘ํ‘œ์‹œ, $ : ๋ํ‘œ์‹œ
.์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ .์œผ๋กœ ๋๋‚˜๋ฉด ""๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

6๋‹จ๊ณ„์—์„œ๋„ ๋งˆ์ง€๋ง‰์ด .์œผ๋กœ ๋๋‚˜๋Š”์ง€ ์ฒดํฌํ•  ๋•Œ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€๋‹ค.

new_id = new_id.replaceAll("[.]$","");

์ตœ์ข… ์†Œ์Šค

class Solution {
    public String solution(String new_id) {
        String answer = "";

        //1๋‹จ๊ณ„
        new_id = new_id.toLowerCase();

        //2๋‹จ๊ณ„
        new_id = new_id.replaceAll("[^0-9a-z._-]", "");

        //3๋‹จ๊ณ„
        new_id = new_id.replaceAll("[.]{2,}", ".");
        
        //4๋‹จ๊ณ„
        new_id = new_id.replaceAll("^[.]|[.]$", "");
        
        //5๋‹จ๊ณ„
        if(new_id.length() == 0){
            new_id = "a";
        }
      
        //6๋‹จ๊ณ„
        if(new_id.length() >= 16){
            new_id = new_id.substring(0, 15);
            new_id = new_id.replaceAll("[.]$","");
        }

        //7๋‹จ๊ณ„
        if(new_id.length() ==2 ){
           new_id = new_id + new_id.charAt(1);
        }else if(new_id.length() ==1){
            new_id = new_id + new_id.charAt(0) + new_id.charAt(0);
        }

        answer = new_id;
        return answer;
    }
}


๋‹ค ํ†ต๊ณผํ–ˆ๋‹ค..! ์ •๊ทœ์‹๊ณผ๋„ ์ข€ ๊ฐ€๊นŒ์›Œ์งˆ ํ•„์š”๊ฐ€...์žˆ๋‹ค....

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€