class MLC:
def __init__(self,s):
n = len(s)
t = [1] + [0] * (2*n+1) + [2]
for i,ch in zip(range(2,2*n+1,2),s):
t[i] = ch
d = [0] * (2*n+3)
m,r = 2,2
for i in range(2,2*n+1):
d[i] = min(d[2*m-i],r-i)
while t[i-d[i]] == t[i+d[i]] :
d[i] += 1
if i+d[i] > r :
r = i + d[i]
m = i
self.d = [x for x in d[1:-1]]
def count(self):
return sum(x >> 1 for x in self.d)
def check(self,i,j):
return self.d[i+j] > j - i
class Trie:
def __init__(self):
self.root = defaultdict(int)
def insert(self, word: str) -> None:
node = self.root
node['path'] += 1
for ch in word:
node = node.setdefault(ch,defaultdict(int))
node['path'] += 1
node['end'] += 1
def search(self, word: str) -> bool:
node = self.root
for ch in word:
if ch not in node or node['path'] <= 0:
return False
node = node[ch]
return node['end'] > 0
def startsWith(self, prefix: str) -> bool:
node = self.root
for ch in prefix:
if ch not in node or node['path'] <= 0:
return False
node = node[ch]
return True