algorithm

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

3hoon 2022. 11. 23. 22:23
class Solution {
    public String solution(String new_id) {
        // 1단계
        String step2 = "";
        step2 = new_id.toLowerCase(); // 모든 문자 소문자로 치환
        // 2단계
        String step3 = step2.replaceAll("[^a-z0-9_.-]", ""); // 정규식을 통한 조건에 맞는 문자제외하고 모두 제거
        // 3단계                                              // a부터z까지, 0부터9까지 _.-제외하고 전부 제거 (replaceAll 함수사용)
        String step4 = "";
        step4 = step3.replaceAll("[.]+", ".");  // 마침표(.)가 2번이상 연속될시 마침표(.) 1개로 치환
        // 4단계
        String step5 = "";
        String[] step5Result = step4.split(""); // 문자열 배열화
        for (int i = 0; i < step5Result.length; i++) {
            if (i == 0 || i == step5Result.length - 1) {  //i인덱스가 첫번째 이거나 마지막 이거나
                step5Result[i] = step5Result[i].replaceAll("\\.", ""); // 마침표(.)가 있을 경우 제거
                step5 += step5Result[i];
            }else {
                step5 += step5Result[i];
            }
        }
        // 5단계
        String step6 = "";
        if (step5.isEmpty() || step5 == null || step5.equals("")) { // 빈 문자열 일 경우 a로 치환
            step6 = "a";
        }else {
            step6 = step5;
        }
        // 6단계
        String step7 = "";
        if (step6.length() >= 16) {
            step7 = step6.substring(0,15);
            String[] step6Result = step7.split("");
            step7 = "";
            for (int i = 0; i < step6Result.length; i++) {
                if (i == step6Result.length - 1) {
                    step6Result[i] = step6Result[i].replaceAll("\\.", "");
                    step7 += step6Result[i];
                }else {
                    step7 += step6Result[i];
                }
            }
        }else {
            step7 = step6;
        }
        // 7단계
        String answer = step7;
        if (answer.length() < 3) {
            String trimString = String.valueOf(answer.charAt(answer.length() - 1));
            while (true) {
                answer += trimString;
                if (answer.length() >= 3) {
                    break;
                }
            }
        }
        return answer;
        }
    }

자세한 정보 클릭

 

GitHub - leemeo3/algorithm: This is a auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.c

This is a auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - GitHub - leemeo3/algorithm: This is a auto push repository for Bae...

github.com

 

총 7단계의 조건이 있고 순차적으로 그 조건을 구현하면 되는 문제였는데

특히 2번이나 3번 조건은 코드를 길어지게 하는 원인이었고 3번의 마침표가 2번 연속 반복될 경우 하나로 치환하는

부분은 도저히 구현이 되질 않아서 고민을 하다가 정규식에 대해서 알게 되었고

저렇게 한줄로 구현이 완료되었다.

정규식에 대한 사용 방법만 안다면 많은 부분의 코딩테스트 기법을 대체할 수 있겠다 라는 생각이 들었고

잘 활용하기 위해선 잘 배우는 것도 중요하기에 앞으로 코딩테스트를 할때 정규식을 가장 먼저 고려해보고

그게 안된다면 다른 방법을 통해 구현하도록 하겠다.

String new_id = "...!@BaT#*..y.abcdefghijklm"; // bat.y.abcdefghi
        String new_id2 = "=.="; // aaa
        String new_id3 = "ab"; // abb
        String new_id4 = "z-+.^."; // z--
        String new_id5 = "a="; // aaa
        String new_id6 = "===a=.!!+!+!a"; // a.a
        String new_id7 = "==={}[][]aa}"; // aaa
        String new_id8 = "........................"; // aaa

위의 테스트케이스를 통해 디버깅을 해서 완료했다.