매 반복마다 윈도우(sub_dna)의 각 문자의 개수를 세고, 윈도우를 오른쪽으로 이동하는 슬라이딩 윈도우를 사용했다.
이때 마지막 문자의 값은 1만큼 줄이고, 새로 들어온 문자의 값은 1만큼 증가시키며 주어진 조건을 만족하는지 확인했다.
# 정답
import sys
# check_condition(): 현재 확인 중인 부분 문자열이 조건을 만족하는지 확인하는 함수
def check_condition(sub_dna, conditions):
if sub_dna['A'] >= conditions[0]:
if sub_dna['C'] >= conditions[1]:
if sub_dna['G'] >= conditions[2]:
if sub_dna['T'] >= conditions[3]:
return True
return False
s, p = map(int, sys.stdin.readline().split())
dna = list(sys.stdin.readline().rstrip())
conditions = list(map(int, sys.stdin.readline().split()))
# result: 민호가 만들 수 있는 비밀번호의 종류의 수
result = 0
# sub_dna: 현재 확인 중인 부분 문자열의 각 문자 개수
sub_dna = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
# 슬라이딩 윈도우를 활용하여, 길이 p인 첫 번째 부분 문자열(dna[0:p])부터 순차적으로 확인한다.
for d in dna[0:p]:
sub_dna[d] += 1
if check_condition(sub_dna, conditions):
result += 1
for i in range(p, s):
# 윈도우에 새로 들어오는 문자
sub_dna[dna[i]] += 1
# 윈도우에서 빠져나가는 문자
sub_dna[dna[i - p]] -= 1
if check_condition(sub_dna, conditions):
result += 1
print(result)