Skip to content

Commit 771701e

Browse files
committed
[level 0] Title: 문자열 밀기, Time: 1.73 ms, Memory: 72.3 MB -BaekjoonHub
1 parent 2353d35 commit 771701e

2 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [level 0] 문자열 밀기 - 120921
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/120921)
4+
5+
### 성능 요약
6+
7+
메모리: 72.3 MB, 시간: 1.73 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 코딩테스트 입문
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 06월 29일 16:15:47
20+
21+
### 문제 설명
22+
23+
<p>문자열 "hello"에서 각 문자를 오른쪽으로 한 칸씩 밀고 마지막 문자는 맨 앞으로 이동시키면 "ohell"이 됩니다. 이것을 문자열을 민다고 정의한다면 문자열 <code>A</code>와 <code>B</code>가 매개변수로 주어질 때, <code>A</code>를 밀어서 <code>B</code>가 될 수 있다면 밀어야 하는 최소 횟수를 return하고 밀어서 <code>B</code>가 될 수 없으면 -1을 return 하도록 solution 함수를 완성해보세요.</p>
24+
25+
<hr>
26+
27+
<h5>제한사항</h5>
28+
29+
<ul>
30+
<li>0 &lt; <code>A</code>의 길이 = <code>B</code>의 길이 &lt; 100</li>
31+
<li><code>A</code>, <code>B</code>는 알파벳 소문자로 이루어져 있습니다.</li>
32+
</ul>
33+
34+
<hr>
35+
36+
<h5>입출력 예</h5>
37+
<table class="table">
38+
<thead><tr>
39+
<th>A</th>
40+
<th>B</th>
41+
<th>result</th>
42+
</tr>
43+
</thead>
44+
<tbody><tr>
45+
<td>"hello"</td>
46+
<td>"ohell"</td>
47+
<td>1</td>
48+
</tr>
49+
<tr>
50+
<td>"apple"</td>
51+
<td>"elppa"</td>
52+
<td>-1</td>
53+
</tr>
54+
<tr>
55+
<td>"atat"</td>
56+
<td>"tata"</td>
57+
<td>1</td>
58+
</tr>
59+
<tr>
60+
<td>"abc"</td>
61+
<td>"abc"</td>
62+
<td>0</td>
63+
</tr>
64+
</tbody>
65+
</table>
66+
<hr>
67+
68+
<h5>입출력 예 설명</h5>
69+
70+
<p>입출력 예 #1</p>
71+
72+
<ul>
73+
<li>"hello"를 오른쪽으로 한 칸 밀면 "ohell"가 됩니다.</li>
74+
</ul>
75+
76+
<p>입출력 예 #2</p>
77+
78+
<ul>
79+
<li>"apple"은 몇 번을 밀어도 "elppa"가 될 수 없습니다.</li>
80+
</ul>
81+
82+
<p>입출력 예 #3</p>
83+
84+
<ul>
85+
<li>"atat"는 오른쪽으로 한 칸, 세 칸을 밀면 "tata"가 되므로 최소 횟수인 1을 반환합니다.</li>
86+
</ul>
87+
88+
<p>입출력 예 #4</p>
89+
90+
<ul>
91+
<li>"abc"는 밀지 않아도 "abc"이므로 0을 반환합니다.</li>
92+
</ul>
93+
94+
<hr>
95+
96+
<p>※ 공지 - 2023년 4월 24일 테스트케이스가 추가되었습니다. 기존에 제출한 코드가 통과하지 못할 수도 있습니다.</p>
97+
98+
99+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(String A, String B) {
5+
6+
if(A.equals(B)) return 0;
7+
8+
int len = A.length();
9+
String tmp = "";
10+
11+
int cnt = 0;
12+
for(int i=0; i<len; i++){
13+
tmp += A.substring(len-1);
14+
tmp += A.substring(0,len-1);
15+
16+
System.out.println("tmp : " + tmp);
17+
18+
cnt++;
19+
if(tmp.equals(B)) break;
20+
else {
21+
A = tmp;
22+
tmp = "";}
23+
}
24+
25+
if(cnt == len) return -1;
26+
else return cnt;
27+
28+
29+
}
30+
}

0 commit comments

Comments
 (0)