p. 370
from bisect import bisect_left, bisect_right
def count_by_value(array, left_q, right_q):
left=bisect_left(array, left_q)
right=bisect_right(array, right_q)
return right-left
def solution(words, queries):
answer = []
# 단어들의 길이 별로 배열 만들기
array=[[] for _ in range(10001)]
reverse_array=[[] for _ in range(10001)]
# 단어의 길이에 맞게 배열에 저장
for word in words:
array[len(word)].append(word)
reverse_array[len(word)].append(word[::-1])
# 배열 정렬
for i in range(10001):
array[i].sort()
reverse_array[i].sort()
for q in queries:
if q[0]=='?': # 접미사
answer.append(count_by_value(reverse_array[len(q)],q[::-1].replace('?','a'),q[::-1].replace('?','z')))
else:
answer.append(count_by_value(array[len(q)],q.replace('?','a'), q.replace('?','z')))
return answer
댓글