forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.py
31 lines (26 loc) Β· 1.48 KB
/
4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from bisect import bisect_left, bisect_right
# κ°μ΄ [left_value, right_value]μΈ λ°μ΄ν°μ κ°μλ₯Ό λ°ννλ ν¨μ
def count_by_range(a, left_value, right_value):
right_index = bisect_right(a, right_value)
left_index = bisect_left(a, left_value)
return right_index - left_index
# λͺ¨λ λ¨μ΄λ€μ κΈΈμ΄λ§λ€ λλμ΄μ μ μ₯νκΈ° μν 리μ€νΈ
array = [[] for _ in range(10001)]
# λͺ¨λ λ¨μ΄λ€μ κΈΈμ΄λ§λ€ λλμ΄μ λ€μ§μ΄ μ μ₯νκΈ° μν 리μ€νΈ
reversed_array = [[] for _ in range(10001)]
def solution(words, queries):
answer = []
for word in words: # λͺ¨λ λ¨μ΄λ₯Ό μ λ―Έμ¬ μμΌλμΉ΄λ λ°°μ΄, μ λμ¬ μμΌλμΉ΄λ λ°°μ΄μ κ°κ° μ½μ
array[len(word)].append(word) # λ¨μ΄λ₯Ό μ½μ
reversed_array[len(word)].append(word[::-1]) # λ¨μ΄λ₯Ό λ€μ§μ΄μ μ½μ
for i in range(10001): # μ΄μ§ νμμ μννκΈ° μν΄ κ° λ¨μ΄ 리μ€νΈ μ λ ¬ μν
array[i].sort()
reversed_array[i].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