https://www.acmicpc.net/problem/16472
import sys
input = sys.stdin.readline
N = int(input())
S = input().strip()
start = 0
end = 0
counter = dict()
dap = 0
# end를 0번째부터 하나씩 넣으면서 카운트 및 알파벳 개수 비교
while end < len(S):
char = S[end]
# 해당 알파벳 dict저장해서 카운트
if char in counter:
counter[char] += 1
else:
counter[char] = 1
# N개 종류 넘으면 N개 종류 안 넘을 때까지
# 처음에 해당하는 알파벳 하나 빼기
while len(counter) > N:
# start 이동
counter[S[start]] -= 1
if counter[S[start]] == 0:
del counter[S[start]]
start += 1
# 기준을 만족하면 길이 구하고 최대값 비교
dap = max(dap, end-start+1)
# end 이동
end += 1
print(dap)'코딩테스트 > 코딩테스트 문제풀이' 카테고리의 다른 글
| [Python] 프로그래머스 <시소 짝꿍> (0) | 2025.07.25 |
|---|---|
| [Python] 백준 <1261-알고스팟> (0) | 2025.07.23 |
| [Python] 백준 <1062-가르침> (0) | 2025.07.23 |
| [Python] 백준 <1034-램프> (0) | 2025.07.21 |
| [Python] 백준 <9663-N-Queen> (0) | 2025.07.17 |