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

1 개요[ | ]

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

2 C++[ | ]

#include <string>
#include <vector>
#include <regex>
using namespace std;

string solution(string new_id) {
    string answer = "";
    char prev = ' ';
    for(char& c: new_id) {
        if(c >='A' && c <= 'Z') c = tolower(c);
        if((c >='a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.') {
            if( c == '.' && c == prev ) {
                prev = c;
                continue;
            }
            answer += c;
            prev = c;
        }
    }
    if(answer[0] == '.') answer = answer.substr(1);
    if(answer[answer.length()-1] == '.') answer = answer.substr(0,answer.length()-1);
    if(answer == "") answer = "a";
    if(answer.length()>=16) {
        answer = answer.substr(0,15);
        if(answer[answer.length()-1] == '.') answer = answer.substr(0,answer.length()-1);
    }
    for(int i=answer.length(); i<3; i++) {
        answer += answer[i-1];
    }
    return answer;
}
#include <regex>
#include <string>
#include <vector>
using namespace std;

string solution(string new_id) {
    for (char& c : new_id) c = tolower(c);
    string answer;
    for (const char& c : new_id) {
        if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' ||
            (c == '.' && (answer.size() == 0 || answer.back() != '.'))) {
            answer += c;
        }
    }
    if (answer.front() == '.') answer.erase(answer.begin());
    if (answer.back() == '.') answer.pop_back();
    if (answer.empty()) answer = "a";
    if (answer.size() >= 16) answer = answer.substr(0, 15);
    if (answer.back() == '.') answer.pop_back();
    while (answer.size() <= 2) answer += answer.back();
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}