나의 풀이
public class Solution {
public String solution(String new_id) {
String answer = "";
//1단계 알파벳을 전부다 소문자로 바꾸어 준다.
String tmp= new_id.toLowerCase();
//2단계 소문자, 숫자, -, _, . 을 제외한 모든 문자를 소거한다.
tmp=tmp.replaceAll("[^a-z0-9\\-_.]", "");
//3단계 마침표가 2개이상인 경우 1개로 대체한다.
tmp=tmp.replaceAll("\\.+", ".");
//4단계 문자열이 마침표로 시작하거나 끝나는 경우에는 앞쪽의 마침표와 뒤쪽의 마침표를 제거해 준다.
tmp=tmp.replaceAll("^[.]|[.]$", "");
//5단계 tmp가 빈 문자열이라면 a를 더한다.
if(tmp.equals("")){
tmp = "a";
}
//6-1단계: 문자열의 길이가 16자 이상인 경우 15자리까지만 슬라이싱한다.
if(tmp.length()>=16) {
tmp = tmp.substring(0, 15);
//6-2단계: 문자열이 마침표로 끝나는 경우 마침표를 제거한다.
tmp=tmp.replaceAll("[.]$","");
}
//7단계 문자열의 길이가 2이하인 경우에는 문자열이 길이가 3이 될때까지 마미막 문자열을 더한다.
if(tmp.length()<=2){
while(tmp.length()<3){
tmp+=tmp.charAt(tmp.length()-1);
}
}
answer=tmp;
return answer;
}
다른 사람의 풀이
class Solution {
public String solution(String new_id) {
String answer = "";
String temp = new_id.toLowerCase();
temp = temp.replaceAll("[^-_.a-z0-9]","");
System.out.println(temp);
temp = temp.replaceAll("[.]{2,}",".");
temp = temp.replaceAll("^[.]|[.]$","");
System.out.println(temp.length());
if(temp.equals(""))
temp+="a";
if(temp.length() >=16){
temp = temp.substring(0,15);
temp=temp.replaceAll("^[.]|[.]$","");
}
if(temp.length()<=2)
while(temp.length()<3)
temp+=temp.charAt(temp.length()-1);
answer=temp;
return answer;
}
}
▶ [^-_.a-z0-9] ==[^a-z0-9\-_.]
▶ \.+ : 마침표가 1개 이상인 경우
▶ [.]{2,} : 마침표가 2개 이상인 경우
'프로그래머스(자바) > LV.1(자바)' 카테고리의 다른 글
옹알이2 -> 포함하고 있지않다면 if !(babb.contains(text+text))★★ (0) | 2022.12.22 |
---|---|
크레인 인형 뽑기 → 열접근<for each문 2개>★★+break문★ (0) | 2022.12.21 |
폰켓몬→ boxed()★+ collectingAndThen() (0) | 2022.12.21 |
키패드 누르기★★ (0) | 2022.12.20 |
숫자 짝궁→char타입 - int타입, 내림차순 정렬시 0이 맨 앞에 오는 경우→ 모든 요소를 '0'으로 되어 있다 . (0) | 2022.12.20 |