💻 문제 출처 : 프로그래머스_신규 아이디 추천
class Solution {
public String solution(String new_id) {
new_id = new_id.toLowerCase();
new_id = new_id.replaceAll("[^a-z0-9-_.]", "");
new_id = new_id.replaceAll("[/.]+", ".");
if(new_id.length() > 0 && new_id.charAt(0) == '.') {
new_id = new_id.substring(1, new_id.length());
}
if(new_id.length() > 0 && new_id.charAt(new_id.length() - 1) == '.') {
new_id = new_id.substring(0, new_id.length() - 1);
}
if(new_id.length() == 0) {
new_id = "a";
}
if(new_id.length() > 15) {
new_id = new_id.substring(0, 15);
}
if(new_id.length() > 0 && new_id.charAt(new_id.length() - 1) == '.') {
new_id = new_id.substring(0, new_id.length() - 1);
}
if(new_id.length() < 3) {
String s = String.valueOf(new_id.charAt(new_id.length() - 1));
new_id += s.repeat(3 - new_id.length());
}
return new_id;
}
}
📌 문제 풀이 설명
class Solution {
public String solution(String new_id) {
String s = new KAKAOID(new_id)
.replaceToLowerCase()
.filter()
.toSingleDot()
.noStartEndDot()
.noBlank()
.noGreaterThan16()
.noLessThan2()
.getResult();
return s;
}
private static class KAKAOID {
private String s;
KAKAOID(String s) {
this.s = s;
}
private KAKAOID replaceToLowerCase() {
s = s.toLowerCase();
return this;
}
private KAKAOID filter() {
s = s.replaceAll("[^a-z0-9._-]", "");
return this;
}
private KAKAOID toSingleDot() {
s = s.replaceAll("[.]{2,}", ".");
return this;
}
private KAKAOID noStartEndDot() {
s = s.replaceAll("^[.]|[.]$", "");
return this;
}
private KAKAOID noBlank() {
s = s.isEmpty() ? "a" : s;
return this;
}
private KAKAOID noGreaterThan16() {
if (s.length() >= 16) {
s = s.substring(0, 15);
}
s = s.replaceAll("[.]$", "");
return this;
}
private KAKAOID noLessThan2() {
StringBuilder sBuilder = new StringBuilder(s);
while (sBuilder.length() <= 2) {
sBuilder.append(sBuilder.charAt(sBuilder.length() - 1));
}
s = sBuilder.toString();
return this;
}
private String getResult() {
return s;
}
}
}
📌 문제 풀이 설명