classSolution:defmodifiedMatrix(self, g: List[List[int]])-> List[List[int]]:
g =list(zip(*g))
ans =[list(v)for v in g]for row,p inzip(g,ans):
mx =max(row)for i,v inenumerate(row):if v ==-1:
p[i]= mx
returnlist(zip(*ans))
classSolution:defmaxPalindromesAfterOperations(self, words: List[str])->int:
cnt = Counter()
ds =[]for w in words:
ds.append(len(w))for c in w:
cnt[c]+=1
one = two =0for v in cnt.values():
one += v %2
two += v //2
ans =0for d insorted(ds):
need_one = d&1# 需要补一个奇数
d //=2if need_one andnot one and two:# 没奇数用偶数补
two -=1
one +=2if one >= need_one and two >= d:# 奇偶都够
one -= need_one
two -= d
ans +=1return ans
classZFunction:def__init__(self, s):
n =len(s)
self.z = z =[0]* n
l, r =0,0for i inrange(1, n):if i <= r and z[i - l]< r - i +1:
z[i]= z[i - l]else:
z[i]=max(0, r - i +1)while i + z[i]< n and s[z[i]]== s[i + z[i]]:
z[i]+=1if i + z[i]-1> r:
l = i
r = i + z[i]-1@staticmethoddeffind_all_match_pos(s, t):"""从s中找到所有t的位置"""
s = t +'#'+ s
n =len(t)
z = ZFunction(s).z
ans =[]for i inrange(n +1,len(s)):if z[i]== n:
ans.append(i - n -1)return ans
classSolution:defcountMatchingSubarrays(self, nums: List[int], pattern: List[int])->int:
s =''.join(['a'if x < y else('b'if x == y else'c')for x,y in pairwise(nums)])
t =''.join(['a'if v ==1else('b'if v ==0else'c')for v in pattern])returnlen(ZFunction.find_all_match_pos(s, t))