From 630d1141c1fa88681628d52e3b77217afadcda19 Mon Sep 17 00:00:00 2001 From: nkey Date: Thu, 19 Feb 2026 14:53:23 +0900 Subject: [PATCH] =?UTF-8?q?1522-s1=20=ED=92=80=EC=9D=B4=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=ED=9B=84=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_8708/1522-s1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 workbook_8708/1522-s1.py diff --git a/workbook_8708/1522-s1.py b/workbook_8708/1522-s1.py new file mode 100644 index 0000000..8034be0 --- /dev/null +++ b/workbook_8708/1522-s1.py @@ -0,0 +1,32 @@ +# 문자열 교환 + +import sys + +input = sys.stdin.readline + +def solution(): + s = input().rstrip() + + k = s.count("a") + k_count = {"a": s[:k].count("a"), "b": s[:k].count("b")} + circle = s + s + result = k_count["b"] + for i in range(1, len(circle)-k): + k_count[circle[i-1]] -= 1 + k_count[circle[i+k-1]] += 1 + result = min(result, k_count["b"]) + + print(result) + return + + +solution() + +""" +걸린 시간: 못 품 + +시간 복잡도: 전체에서 a의 개수(k)를 세고, k구간씩 옮겨가며 b개수를 슬라이딩 윈도우로 업데이트하기 때문에 전체 시간복잡도는 O(n)이다. + +해설: 원형이라고 했기 때문에 같은 것 2배를 해준 문자열을 만든다. 여기서 기존 a가 다 연속이면 되기 때문에 +a의 개수 k를 구해서 원형에서 k구간씩 확인하며 b를 다 빼주면 되는데, 이때 b개수의 최소값이 답이다. +""" \ No newline at end of file