一直是三题选手,不知道什么时候能突破
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
k = self.sqrt(k * 4)
cnt = Counter([(k - 1, 0)]) # k-1 和 -1 同余
ans = pre_sum = 0
for i, v in enumerate(s):
pre_sum += 1 if v in "aeiou" else -1
p = (i % k, pre_sum)
ans += cnt[p]
cnt[p] += 1
return ans
def sqrt(self, n: int) -> int:
res = 1
i = 2
while i * i <= n:
i2 = i * i
while n % i2 == 0:
res *= i
n //= i2
if n % i == 0:
res *= i
n //= i
i += 1
if n > 1:
res *= n
return res
作者:灵茶山艾府
链接:https://leetcode.cn/circle/discuss/ip8jWQ/view/hLnPbj/
来源:力扣(LeetCode)