def solution(S, P, Q):
l = len(P)
answer =[]
dic = {'A':1,'C':2,'G':3,'T':4}
for i in range(l):
m = dic[S[P[i]]]
for s in set(S[P[i]:Q[i]+1]):
m = min(dic[s],m)
answer.append(m)
return answer
from collections import defaultdict
def solution(S, P, Q):
l = len(S) + 1
prefix_dic = defaultdict(list)
cur = {'A': 0, 'C' : 0, 'G':0, 'T':0 }
answer = []
prefix_dic = {'A':[0]*l, 'C':[0]*l, 'G':[0]*l, 'T':[0]*l}
for i, s in enumerate(S):
cur[s] += 1
prefix_dic['A'][i+1] += cur['A']
prefix_dic['C'][i+1] += cur['C']
prefix_dic['G'][i+1] += cur['G']
prefix_dic['T'][i+1] += cur['T']
for p, q in zip(P,Q):
if prefix_dic['A'][q+1] - prefix_dic['A'][p] :
answer.append(1)
elif prefix_dic['C'][q+1] - prefix_dic['C'][p] :
answer.append(2)
elif prefix_dic['G'][q+1] - prefix_dic['G'][p] :
answer.append(3)
else:
answer.append(4)
return answer
len(S)+1 을 해주는 이유는 현재 값에서 변화량을 알고 싶기 때문