Study Record

[프로그래머스] Level1 - 신규 아이디 본문

알고리즘

[프로그래머스] Level1 - 신규 아이디

초코초코초코 2021. 11. 30. 01:28
728x90
#include <string>
#include <vector>

using namespace std;

string solution(string new_id) {
    string answer = "";
    
    // 1,2,3 단계
    for(int i=0; i<new_id.length(); i++){
        char word = new_id[i];
        if(word >= 'A' && word <= 'Z'){
            answer += word + 'a' - 'A';        
        } else {
            // 2단계
            if( (word >= 'a' && word <= 'z') ||
               (word >= '0' && word <= '9') ||
               word == '-' || word == '_' || word == '.'){
                answer += word;
            } 
        }
    }
    
    // 3단계
    string new_answer = "";
    for(int i=0; i<answer.length(); i++){
        new_answer += answer[i];
        if(answer[i] == '.'){
            int num_dot = 1;
            for(int k = i+1; k < answer.length(); k++){
                if(answer[k] == '.') num_dot++;
                else break;
            }
            i = i + num_dot - 1;   
        }
    }
    answer = new_answer;
    
    // 4단계
    if(answer.length() != 0){ 
        if(answer[0] == '.') answer = answer.substr(1, answer.length());
        if(answer[answer.length()-1] == '.') answer = answer.substr(0, answer.length()-1);
    }
    
    // 5단계
    if(answer.length() == 0){
        answer = 'a';    
    }
    
    // 6단계
    if(answer.length() >= 16){
        answer = answer.substr(0, 15);
        if(answer[answer.length() - 1] == '.') answer = answer.substr(0, answer.length()-1);
    }
    
    // 7단계
    if(answer.length() <= 2){
        char endText = answer[answer.length() - 1];
        while(answer.length() != 3) answer += endText;
    }
    
    return answer;
}
728x90