문제
https://www.acmicpc.net/problem/20437
코드
from collections import defaultdict
t = int(input())
for _ in range(t):
st = input()
n = int(input())
dict = defaultdict(list)
for index, value in enumerate(st):
dict[value].append(index)
result3 = 10001
result4 = -1
for i in dict:
if len(dict[i]) >= n:
for j in range(len(dict[i])):
if j + n <= len(dict[i]):
result3 = min(result3, dict[i][n + j - 1] - dict[i][j])
result4 = max(result4, dict[i][n + j - 1] - dict[i][j])
if result3 == 10001 and result4 == -1:
print(-1)
else:
print(result3 + 1, end=" ")
print(result4 + 1)
생각 정리
문자별 index를 dict에 넣어주고 dict을 sliding window 해주었다.
'문제풀이 > 기타' 카테고리의 다른 글
[파이썬] [문자열] 백준 17413 단어 뒤집기 2 (0) | 2023.01.09 |
---|---|
[파이썬] [문자열] 백준 17609 회문 (0) | 2023.01.07 |
@[파이썬] [문자열] 백준 3107 IPv6 (0) | 2023.01.04 |
[파이썬] [문자열] 백준 1764 듣보잡 (0) | 2023.01.03 |
[파이썬] [재귀] 백준 2447 별 찍기 - 10 (0) | 2023.01.02 |