Skip to content

Commit 591a0d3

Browse files
committed
[level 1] Title: 신규 아이디 추천, Time: 0.95 ms, Memory: 75.2 MB -BaekjoonHub
1 parent 857c348 commit 591a0d3

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# [level 1] 신규 아이디 추천 - 72410
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/72410)
4+
5+
### 성능 요약
6+
7+
메모리: 75.2 MB, 시간: 0.95 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2021 KAKAO BLIND RECRUITMENT
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 06월 23일 21:28:36
20+
21+
### 문제 설명
22+
23+
<p>카카오에 입사한 신입 개발자 <code>네오</code>는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로 가입하는 유저들이 카카오 아이디 규칙에 맞지 않는 아이디를 입력했을 때, 입력된 아이디와 유사하면서 규칙에 맞는 아이디를 추천해주는 프로그램을 개발하는 것입니다.<br>
24+
다음은 카카오 아이디의 규칙입니다.</p>
25+
26+
<ul>
27+
<li>아이디의 길이는 3자 이상 15자 이하여야 합니다.</li>
28+
<li>아이디는 알파벳 소문자, 숫자, 빼기(<code>-</code>), 밑줄(<code>_</code>), 마침표(<code>.</code>) 문자만 사용할 수 있습니다.</li>
29+
<li>단, 마침표(<code>.</code>)는 처음과 끝에 사용할 수 없으며 또한 연속으로 사용할 수 없습니다.</li>
30+
</ul>
31+
32+
<p>"네오"는 다음과 같이 7단계의 순차적인 처리 과정을 통해 신규 유저가 입력한 아이디가 카카오 아이디 규칙에 맞는 지 검사하고 규칙에 맞지 않은 경우 규칙에 맞는 새로운 아이디를 추천해 주려고 합니다.<br>
33+
신규 유저가 입력한 아이디가 <code>new_id</code> 라고 한다면,</p>
34+
<div class="highlight"><pre class="codehilite"><code>1단계 new_id의 모든 대문자를 대응되는 소문자로 치환합니다.
35+
2단계 new_id에서 알파벳 소문자, 숫자, 빼기(-), 밑줄(_), 마침표(.)를 제외한 모든 문자를 제거합니다.
36+
3단계 new_id에서 마침표(.)가 2번 이상 연속된 부분을 하나의 마침표(.)로 치환합니다.
37+
4단계 new_id에서 마침표(.)가 처음이나 끝에 위치한다면 제거합니다.
38+
5단계 new_id가 빈 문자열이라면, new_id에 "a"를 대입합니다.
39+
6단계 new_id의 길이가 16자 이상이면, new_id의 첫 15개의 문자를 제외한 나머지 문자들을 모두 제거합니다.
40+
만약 제거 후 마침표(.)가 new_id의 끝에 위치한다면 끝에 위치한 마침표(.) 문자를 제거합니다.
41+
7단계 new_id의 길이가 2자 이하라면, new_id의 마지막 문자를 new_id의 길이가 3이 될 때까지 반복해서 끝에 붙입니다.
42+
</code></pre></div>
43+
<hr>
44+
45+
<p>예를 들어, new_id 값이 "...!@BaT#*..y.abcdefghijklm" 라면, 위 7단계를 거치고 나면 new_id는 아래와 같이 변경됩니다.</p>
46+
47+
<p>1단계 대문자 'B'와 'T'가 소문자 'b'와 't'로 바뀌었습니다.<br>
48+
<code>"...!@BaT#*..y.abcdefghijklm"</code> → <code>"...!@bat#*..y.abcdefghijklm"</code></p>
49+
50+
<p>2단계 '!', '@', '#', '*' 문자가 제거되었습니다.<br>
51+
<code>"...!@bat#*..y.abcdefghijklm"</code> → <code>"...bat..y.abcdefghijklm"</code></p>
52+
53+
<p>3단계 '...'와 '..' 가 '.'로 바뀌었습니다.<br>
54+
<code>"...bat..y.abcdefghijklm"</code> → <code>".bat.y.abcdefghijklm"</code></p>
55+
56+
<p>4단계 아이디의 처음에 위치한 '.'가 제거되었습니다.<br>
57+
<code>".bat.y.abcdefghijklm"</code> → <code>"bat.y.abcdefghijklm"</code></p>
58+
59+
<p>5단계 아이디가 빈 문자열이 아니므로 변화가 없습니다.<br>
60+
<code>"bat.y.abcdefghijklm"</code> → <code>"bat.y.abcdefghijklm"</code></p>
61+
62+
<p>6단계 아이디의 길이가 16자 이상이므로, 처음 15자를 제외한 나머지 문자들이 제거되었습니다.<br>
63+
<code>"bat.y.abcdefghijklm"</code> → <code>"bat.y.abcdefghi"</code></p>
64+
65+
<p>7단계 아이디의 길이가 2자 이하가 아니므로 변화가 없습니다.<br>
66+
<code>"bat.y.abcdefghi"</code> → <code>"bat.y.abcdefghi"</code></p>
67+
68+
<p>따라서 신규 유저가 입력한 new_id가 "...!@BaT#*..y.abcdefghijklm"일 때, 네오의 프로그램이 추천하는 새로운 아이디는 "bat.y.abcdefghi" 입니다.</p>
69+
70+
<hr>
71+
72+
<h4><strong>[문제]</strong></h4>
73+
74+
<p>신규 유저가 입력한 아이디를 나타내는 new_id가 매개변수로 주어질 때, "네오"가 설계한 7단계의 처리 과정을 거친 후의 추천 아이디를 return 하도록 solution 함수를 완성해 주세요.</p>
75+
76+
<h4><strong>[제한사항]</strong></h4>
77+
78+
<p>new_id는 길이 1 이상 1,000 이하인 문자열입니다.<br>
79+
new_id는 알파벳 대문자, 알파벳 소문자, 숫자, 특수문자로 구성되어 있습니다.<br>
80+
new_id에 나타날 수 있는 특수문자는 <code>-_.~!@#$%^&amp;*()=+[{]}:?,&lt;&gt;/</code> 로 한정됩니다.</p>
81+
82+
<hr>
83+
84+
<h5><strong>[입출력 예]</strong></h5>
85+
<table class="table">
86+
<thead><tr>
87+
<th>no</th>
88+
<th>new_id</th>
89+
<th>result</th>
90+
</tr>
91+
</thead>
92+
<tbody><tr>
93+
<td>예1</td>
94+
<td><code>"...!@BaT#*..y.abcdefghijklm"</code></td>
95+
<td><code>"bat.y.abcdefghi"</code></td>
96+
</tr>
97+
<tr>
98+
<td>예2</td>
99+
<td><code>"z-+.^."</code></td>
100+
<td><code>"z--"</code></td>
101+
</tr>
102+
<tr>
103+
<td>예3</td>
104+
<td><code>"=.="</code></td>
105+
<td><code>"aaa"</code></td>
106+
</tr>
107+
<tr>
108+
<td>예4</td>
109+
<td><code>"123_.def"</code></td>
110+
<td><code>"123_.def"</code></td>
111+
</tr>
112+
<tr>
113+
<td>예5</td>
114+
<td><code>"abcdefghijklmn.p"</code></td>
115+
<td><code>"abcdefghijklmn"</code></td>
116+
</tr>
117+
</tbody>
118+
</table>
119+
<h5><strong>입출력 예에 대한 설명</strong></h5>
120+
121+
<hr>
122+
123+
<p><strong>입출력 예 #1</strong><br>
124+
문제의 예시와 같습니다.</p>
125+
126+
<p><strong>입출력 예 #2</strong><br>
127+
7단계를 거치는 동안 new_id가 변화하는 과정은 아래와 같습니다.</p>
128+
129+
<p>1단계 변화 없습니다.<br>
130+
2단계 <code>"z-+.^."</code> → <code>"z-.."</code><br>
131+
3단계 <code>"z-.."</code> → <code>"z-."</code><br>
132+
4단계 <code>"z-."</code> → <code>"z-"</code><br>
133+
5단계 변화 없습니다.<br>
134+
6단계 변화 없습니다.<br>
135+
7단계 <code>"z-"</code> → <code>"z--"</code></p>
136+
137+
<p><strong>입출력 예 #3</strong><br>
138+
7단계를 거치는 동안 new_id가 변화하는 과정은 아래와 같습니다.</p>
139+
140+
<p>1단계 변화 없습니다.<br>
141+
2단계 <code>"=.="</code> → <code>"."</code><br>
142+
3단계 변화 없습니다.<br>
143+
4단계 <code>"."</code> → <code>""</code> (new_id가 빈 문자열이 되었습니다.)<br>
144+
5단계 <code>""</code> → <code>"a"</code><br>
145+
6단계 변화 없습니다.<br>
146+
7단계 <code>"a"</code> → <code>"aaa"</code></p>
147+
148+
<p><strong>입출력 예 #4</strong><br>
149+
1단계에서 7단계까지 거치는 동안 new_id("123_.def")는 변하지 않습니다. 즉, new_id가 처음부터 카카오의 아이디 규칙에 맞습니다.</p>
150+
151+
<p><strong>입출력 예 #5</strong><br>
152+
1단계 변화 없습니다.<br>
153+
2단계 변화 없습니다.<br>
154+
3단계 변화 없습니다.<br>
155+
4단계 변화 없습니다.<br>
156+
5단계 변화 없습니다.<br>
157+
6단계 <code>"abcdefghijklmn.p"</code> → <code>"abcdefghijklmn."</code> → <code>"abcdefghijklmn"</code><br>
158+
7단계 변화 없습니다.</p>
159+
160+
161+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Solution {
2+
public String solution(String new_id) {
3+
StringBuilder sb = new StringBuilder();
4+
for (int i = 0; i < new_id.length(); i++) {
5+
sb.append(Character.toLowerCase(new_id.charAt(i)));
6+
}
7+
8+
StringBuilder temp = new StringBuilder();
9+
for (int i = 0; i < sb.length(); i++) {
10+
char c = sb.charAt(i);
11+
12+
if (Character.isLetterOrDigit(c)
13+
|| c == '-'
14+
|| c == '_'
15+
|| c == '.') {
16+
temp.append(c);
17+
}
18+
}
19+
sb = temp;
20+
21+
temp = new StringBuilder();
22+
for (int i = 0; i < sb.length(); i++) {
23+
char c = sb.charAt(i);
24+
25+
if (c == '.') {
26+
if (temp.length() > 0 && temp.charAt(temp.length() - 1) == '.') {
27+
continue;
28+
}
29+
}
30+
31+
temp.append(c);
32+
}
33+
sb = temp;
34+
35+
while (sb.length() > 0 && sb.charAt(0) == '.') {
36+
sb.deleteCharAt(0);
37+
}
38+
39+
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '.') {
40+
sb.deleteCharAt(sb.length() - 1);
41+
}
42+
43+
if (sb.length() == 0) {
44+
sb.append('a');
45+
}
46+
47+
if (sb.length() >= 16) {
48+
sb.setLength(15);
49+
50+
if (sb.charAt(sb.length() - 1) == '.') {
51+
sb.deleteCharAt(sb.length() - 1);
52+
}
53+
}
54+
55+
while (sb.length() < 3) {
56+
sb.append(sb.charAt(sb.length() - 1));
57+
}
58+
59+
return sb.toString();
60+
}
61+
}

0 commit comments

Comments
 (0)