Trie 역방향, 정방향검색, 길이별 검색.
class Node:
def __init__(self):
self.child = {}
self.count = 0
class Trie:
def __init__(self):
self.root = Node()
def insert(self,word):
node = self.root
for char in word:
if char not in node.child:
node.child[char] = Node()
node.count += 1
node = node.child[char]
def startswitch(self,prefix):
node = self.root
for char in prefix:
if char=='?':
return node.count
if char not in node.child:
return 0
node = node.child[char]
return node.count
def solution(words, queries):
trielength = {i: Trie() for i in range(1,10001)}
re_trielength = {i: Trie() for i in range(1,10001)}
answer = []
for word in words:
length = len(word)
trielength[length].insert(word)
re_trielength[length].insert(word[::-1])
for query in queries:
length = len(query)
if query[0] !='?':
answer.append(trielength[length].startswitch(query))
else:
answer.append(re_trielength[length].startswitch(query[::-1]))
return answer
'practivceAlgorithm > programmers' 카테고리의 다른 글
[Programmers][Python] 퍼즐조각채우기 (0) | 2021.10.14 |
---|---|
[Programers][Python] 부족한 금액 계산하기 (0) | 2021.10.09 |
[프로그래머스][Python] 입실 퇴실 (0) | 2021.09.13 |
[프로그래머스][Python] 2018카카오블라인드1차: 캐시 (0) | 2021.09.02 |
[Programmers] 카카오기출2018 뉴스 클러스터링. (0) | 2021.08.31 |