💡 시간 초과 발생 방지를 위해서 시작전 시간 계산을 간략하게 진행하고 들어가자!!

< 시간초과 발생 코드 >
import sys
while(1):
m = int(sys.stdin.readline())
if m == 0:
break
sentence = list(sys.stdin.readline())
result = [0] * (len(sentence) - m)
for i in range(len(sentence)-m):
word = [None] * m
idx = 0
num = 0
for j in range(i,len(sentence)):
if num == m and sentence[j] not in word:
# append는 시간을 많이 잡아먹기 때문에, 배열의 크기를 알면 배열의 크기를 정해두고 받아보자
# result.append(j-i)
result[i] = j-i
break
if sentence[j] in word:
continue
else:
word[idx] = sentence[j]
idx += 1
num += 1
print(max(result))
💡 도무지 생각 나지 않아서, 예시 답안을 참고
=> "투포인터" or "슬라이딩 윈도우" 사용 필요

< 해결코드 >
import sys
from collections import defaultdict
while(True):
m = int(sys.stdin.readline().rstrip())
if m == 0:
break
# 투포인터의 시작과 끝점
start = 0
end = 0
# 총 갯수
cnt = 0
# 범주 내에 문자의 개수를 저장
# dic = dict()
dic = defaultdict(int)
sentence = list(sys.stdin.readline().rstrip())
while(end < len(sentence)):
if len(dic) < m:
# dic[sentence[end]] = dic.get(sentence[end], 0) + 1
# dict가 defaultdict로 선언될 경우 없으면 자동으로 0으로 채워줌
dic[sentence[end]] += 1
end += 1
else:
if sentence[end] in dic:
dic[sentence[end]] += 1
end += 1
else:
dic[sentence[start]] -= 1
if dic[sentence[start]] == 0:
del dic[sentence[start]]
start += 1
if len(dic) <= m:
cnt = max(cnt, end-start)
print(cnt)