Close
Close full mode
logo만렙 개발자 키우기

신규 아이디 추천

Git RepositoryEdit on Github
Last update: a year ago by nowwaterReading time: 2 min

문제

코드

코드 보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class recruit2021_1 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
String line = br.readLine();
Solution sol = new Solution();
System.out.println(sol.solution(line));
}
}
class Solution {
public String solution(String new_id) {
StringBuilder sb = new StringBuilder(new_id);
/* First */
for (int i = 0; i < sb.length(); i++) {
char temp = sb.charAt(i);
if (temp >= 'A' && temp <= 'Z') {
sb.setCharAt(i, (char) ('a' + (temp - 'A')));
}
}
/* Second */
for (int i = 0; i < sb.length(); i++) {
char temp = sb.charAt(i);
if (!((temp >= 'a' && temp <= 'z') || (temp >= '0' && temp <= '9') ||
temp == '-' || temp == '_' || temp == '.')) {
sb.deleteCharAt(i);
i -= 1;
}
}
/* Third */
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '.') {
if (i + 1 < sb.length() && sb.charAt(i + 1) == '.') {
sb.deleteCharAt(i + 1);
i -= 1;
}
}
}
/* Fourth */
while (sb.length() > 0 && (sb.charAt(0) == '.' || sb.charAt(sb.length() - 1) == '.')) {
if (sb.charAt(0) == '.') {
sb.deleteCharAt(0);
}
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '.') {
sb.deleteCharAt(sb.length() - 1);
}
}
/* Fifth */
if (sb.length() == 0) {
sb = new StringBuilder("a");
}
new_id = sb.toString();
/* Sixth */
if (new_id.length() >= 16) {
new_id = new_id.substring(0, 15);
}
sb = new StringBuilder(new_id);
while (sb.charAt(sb.length() - 1) == '.') {
sb.deleteCharAt(sb.length() - 1);
}
/* Seventh */
char last = sb.charAt(sb.length() - 1);
while (sb.length() <= 2) {
sb.append(last);
}
return sb.toString();
}
}

⭐️느낀점⭐️

정규식을 알면 정말 쉽게 풀 수 있었을 것 같다.

정규식을 공부해봐야겠다.

풀이 📣


1️⃣ 문자열의 대문자들을 소문자로 바꿔준다.

- new_id.toLowerCase()

2️⃣ 정규식을 통해 2단계~4단계를 해결해준다.

- 2단계 ) temp = temp.replaceAll("[^-_.a-z0-9]","");
- 3단계 ) temp = temp.replaceAll("[.]{2,}",".");
- 4단계 ) temp = temp.replaceAll("^[.]|[.]$","");

3️⃣ 만약 문자열이 빈 문자열이라면 a 를 추가해주고, 글자 수가 16자 이상일 경우 15글자까지만 자른다. 시작과 끝에 점이 있다면 제거해준다.

- if(temp.equals("")) emp+="a";
- if(temp.length() >=16){ temp = temp.substring(0,15); }
- temp=temp.replaceAll("^[.]|[.]$","");

4️⃣ 2글자 이하라면 마지막 글자를 글자 수가 3이 될 때 까지 붙여주면 추천 아이디가 완성된다.


실수 😅

  • replaceAll 메서드를 제대로 활용하지 못했다.
👩‍💻 Problem Solving — Previous
K 20203
Next — 👩‍💻 Problem Solving
메뉴 리뉴얼