defsolution(words, queries): array = defaultdict(list) reversed_array = defaultdict(list) answer = [] for word in words: array[len(word)].append(word) reversed_array[len(word)].append(word[::-1]) for k in array.keys(): array[k].sort() reversed_array[k].sort() for q in queries: if q[0] != '?': res = count_by_range(array[len(q)], q.replace('?', 'a'), q.replace('?', 'z')) else: res = count_by_range(reversed_array[len(q)], q[::-1].replace('?', 'a'), q[::-1].replace('?', 'z')) answer.append(res) return answer
예를들어 fro??를 찾는다면 froaa ~ frozz 사이에 있는 원소의 개수를 찾는다. (정렬이 되어있으므로 가능)