Skip to content

Commit 1491c09

Browse files
committed
[백준] 문자열 교환 / 실버 1 / 60분 힌트 사용
https://www.acmicpc.net/problem/1522
1 parent 4f793f1 commit 1491c09

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
1. 문자열 입력받기
3+
2. a의 개수만큼 윈도우 크기 가져간 후
4+
3. 하나씩 옆으로 이동하면서 윈도우 안의 b가 가장 최소가 되는 순간을 구함
5+
'''
6+
7+
8+
def countB(str):
9+
cnt = 0
10+
for char in str:
11+
if char == 'b':
12+
cnt += 1
13+
14+
return cnt
15+
16+
17+
str = input()
18+
cnt = 0
19+
20+
# 답 저장 변수
21+
ans = int(1e9)
22+
23+
for char in str:
24+
if char == 'a':
25+
cnt += 1
26+
27+
28+
# a의 개수만큼 문자열 순회하면서 윈도우 안의 b가 몇개인지 확인
29+
for i in range(len(str)):
30+
# 원형을 고려하여 i + cnt 문자열의 길이를 넘어가면 앞에서 가져옴
31+
if i + cnt > len(str):
32+
tmp = str[i:]
33+
tmp2 = str[0: (i + cnt)-len(str)]
34+
b_cnt = countB(tmp + tmp2)
35+
else:
36+
tmp = str[i:i + cnt]
37+
b_cnt = countB(tmp)
38+
ans = min(ans, b_cnt)
39+
40+
print(ans)

0 commit comments

Comments
 (0)